• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the difference between String and other reference types?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we don't create objects of String using new keyword but to create objects of other reference type new is used.so what is the difference between other reference types and String?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pavithra Hennayaka wrote:we don't create objects of String using new keyword but to create objects of other reference type new is used.so what is the difference between other reference types and String?


First, your statement is incorrect; you can create Strings using new, it's just not advisable in many cases.

And the basic answer to your question is: essentially, nothing. A String is a reference type; the only thing the language has done is to allow a few extra goodies such as literals and the overloaded '+' operator to make them easier to use. There's also the String pool, but String isn't the only class to have cached values.

Winston
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Pavithra Hennayaka
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is only special because otherwise you would have to do a lot of extra work. Consider the following example:
If String weren't special, you would have to write the following:
As you see that looks bad, and it takes too long to write. Therefore, the creators of Java (and several other languages as well; even C has string literals even though it doesn't have a string type) decided that String will be like any other object, but with a few syntactic extras like String literals and String concatenation with +. They also added the String pool to prevent a new object to be created for each String literal. As far as I know, those are the only differences.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic