This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

String Memory Question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
can any body explain me why the output of this coming out only "same values"...
because what i read is that JVM creats a pool memory for x1 ,x2 and for "c"...
After concatenation the resulting value of x2 is same as that of reference x1 i.e "abc"

so why the comparision is giving false result....


public class Simple {

public static void main(String[] args) {
String x1 = "abc";
String x2 = "ab";
x2=x2+"c";
System.out.println(x2);

if ( x1 == x2 )
{ // comparing reference vars
System.out.println("reference comparision");
}

if ( x1.equals(x2) ) { // comparing values
System.out.println("same values");
}

}
}
Regards
Hrushikesh
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'+' uses StringBuffer. StringBuffer.toString() creates a 'new' string. You can find this info in the API. I must admit to not knowing this behavior until I checked just now.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because your understanding is wrong... The compiler will internalise constant string references, but the addition of the two strings is not a constant reference anymore.

Essentially, at compile time, you get three String instances created and put aside: "abc", "ab", and "c". When you assign x2 and "c" together, you get a _new_ String. (If you'd actually done "ab" + "c", however, the compiler would almost certainly have optimsed it out to just "abc").

You can "intern" a String to get the internalised reference. Try this:



For more, see the JavaDoc.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this example is clearer:

Running this shows that x1 and x4 refer to the *same* string, because of
compile-time simplification: "ab" + "c" becomes "abc".
On the other hand, x1 and x3 refer to *different* strings, because the
concatenation x2 + "c" is done at run-time. The resulting string is
not in the interned string pool, so it can not be "==" to a literal
string like "abc".
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr. C Lamont Gilbert:
'+' uses StringBuffer. StringBuffer.toString() creates a 'new' string. You can find this info in the API. I must admit to not knowing this behavior until I checked just now.



Given that you just learned the fact, I thought I should clarify for you. The String concatenation operator (+ with at least one String operand) does not use StringBuffer. In fact, this behaviour is implementation dependant, since it is unspecified. Most (but not all) Sun implementations compile to use a StringBuffer when one of the operands to the concatenation operator is a non-constant (JLS 15.28 - not to be confused with a non-final*). The Sun 1.5 implementation uses StringBuilder for this case, not StringBuffer. I'll bite my tongue regarding the IBM 1.5 implementation, since it is not yet public. When both operands are constants, the value is inlined at compile-time i.e. there is no StringBuffer/StringBuilder. This behaviour is specified, and therefore, must occur on all implementations.

*When a final is not a constant
http://jqa.tmorris.net/faq/GetQAndA.action?qids=13
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic