aspose file tools*
The moose likes Beginning Java and the fly likes String question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String question" Watch "String question" New topic
Author

String question

salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
String s1 = new String("abcd");
String s1 = "abcd"
what is the difference between two ?
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
They're identical
Since Strings are a very common thing, sun gave us a convenient
way to create the instance of a string object.
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
Bogus
the instance of the String class
kinda, sortof object, baaah
leaves room crying
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
They're identical
Nope. The difference is, the compiler keeps a cache of all String literals for performance and to help save some memory. Otherwise in your example the compiler would need to create two instances of "abcd". Instead, it creates an instance on the call to new String("abcd"); of just the literal and puts it into the String pool. That instance is used in the copy constructor of String to create a new String object that is stored on the heap just like any other object. Now on the next line String s1 = "abcd";, no new objects are created since the compiler already has "abcd" in the pool, it just sets s1 to refer to that instance. To prove it, try this:

Here are the results:

Note that s1 is not equal to s2, even though they were constructed the same, but s3 and s4 are because they point to the literal "abcd" stored in the String pool.
[ February 21, 2004: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi..ur example is fine....but i have some query..
suppose i am simply writing

look, i have not usued the "new" keyword.
now question is : is it taking the help of "new" keyword internally (not visible) to create the instance ?
if your answer is "yes" ...then System.out.println(s2==s1); can not give result "true"....bcoz they might be in two different memory location...right?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
now question is : is it taking the help of "new" keyword internally (not visible) to create the instance ?
Well, kinda. Did you read what I posted above:

Lines 0-5 are the ones that do the work. ldc #2 loads the literal "abcd" into the accumulator and astore_1 and astore_2 store the reference to that String object to s1 and s2 respectively. Whether or not the compiler actually uses new or not to create the literal "abcd", I can't say for sure, but my guess would be no, that it instead uses an internal native method to create the String.
[ February 21, 2004: Message edited by: Michael Morris ]
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi
your comment

Whether or not the compiler actually uses new or not to create the literal "abcd", I can't say for sure, but my guess would be no, that it instead uses an internal native method to create the String.

....hmmm, you know , i am a beginner....well, it was not known to me, that objects can be created without "new" keywords...All i know , if you want to create any object then you must use "new"
keyword.
anyway, in this case both the strings are pointing to the same string pool automatically without assignments!!..... which was not possible if i usued "new" keyword.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
....hmmm, you know , i am a beginner
Sorry if I was less than diplomatic in my answer. Strings are a special case that all Java programmers should understand. There are some other instances where new is not used to create objects, for example when a ClassLoader creates a Class object it calls a native method to create it, but for ordinary Java programming it is not necessary to understand that.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
salvador,

but remember, this is just the == operator not the equals() method.
Davy


How simple does it have to be???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String question