• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = new String("abcd");
String s1 = "abcd"
what is the difference between two ?
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bogus
the instance of the String class
kinda, sortof object, baaah
leaves room crying
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....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.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
salvador,

but remember, this is just the == operator not the equals() method.
Davy
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic