• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Memory Question  RSS feed

 
bhadule bhadule
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Robert Watkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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".
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!