• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

== Operator again

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain this when objects are created(runtime,compile time)
String st0 = "JAVA";
System.out.println(st0 == "JA" + "VA"); // prints true;

String st1 = "JAVA";
String st2 = "JA";
String st3 = "VA";
System.out.println(st1 == st2 + st3); // prints false;
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first case "Ja" + "va" it is solved at compile time.
In second case
String st1 = "JAVA";
String st2 = "JA";
String st3 = "VA";
System.out.println(st1 == st2 + st3); It is computed at runtime and "Strings computed at runtime are newly created and therefore distinct.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the compiler sees literals like
"JA" + "VA"
or 2 * 3.14159
that can be evaluated at once, it does the evaluation, so your example is simplified by the compiler to
"JAVA"
Bill
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Literals are created in the Constant Pool and are resolved at compile time and created at class load time. They are also "re-used".
stO points to the "JAVA" literal.
"JA" + "VA" points to the SAME literal. The References are the same.
st1 points to "JAVA".
st2 points to a different literal "JA"
st3 points to "VA"
The system does not resolve the concatenating of the two variable references in st2 and st3 until runtime, so it does not use the Constant Pool because that has already been set in stone and the system does not KNOW that an appropriate literal will exist there. So it makes another string instead.
[This message has been edited by Cindy Glass (edited February 28, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I originaly said that the new string would be created on the heap. Then it bugged me ALL LAST NIGHT. So I took that statement out.
I don't believe that I can say FOR A FACT that every JVM would handle the new string the same way.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic