Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

string constant pool  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
pg 384 in Kathy's book has this code snippet:
String x1 = "abc";
String x2 = "ab";
x2 = x2 + "c";
if (x1!=x2)
System.out.println("Different objects);
--------------------------------------------
This complies and prints "Different objects".
which means x1 and x2 do not refer to the same object.
My question is:
while compiling line 3 and trying to create a new object and assigning to x2, shouldn't the VM first check in the string constant pool to look for a string "abc" and make x2 refer to the string "abc" already there in the pool ??
that way x1 and x2 would refer to the same object "abc" in the constant pool.
 
Sharda R Govindu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am guessing two things here, please correct me if i'm wrong.
1) The VM searches the constant pool for "abc" only when it sees something like:
String s2 = "abc"; // direct initializing.
in the code.
2) For all other statements like :
String s3 = s2 + "c"; //( if s2 is assigned "ab" as a value), VM concatenates "c" to s2 and creates a new string object without looking for an existing string in the pool.( like what it does when VM sees something like String s2 = new String("abc"), it creates a new object "abc" even if there is an existing string "abc" in the pool).
Could anybody please verify ??
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. In your first example, the expression x2 = x2 + "c" is evaluated at the run time and thus a new instance of "abc" is created and referred to x2 and that's why the references x1 and x2 are not equal.
2. Similarly, your second example, the String s3 = s2 +"c" is happening in the run time and thus a new String instance is created for s3. But if String s2 were to be final, then s3 = s2 + "c" whould rather be seen as s3 = "ab" + "c" at the compile time and then s3 would refer to the same string "abc" in the pool.
In such a case...
String s1 = "abc";
final String s2 = "ab";
String s3 = s2 + "c";
s1 == s3 will be true;
Hope this helps!
[ April 26, 2003: Message edited by: Vidya Ram ]
[ April 26, 2003: Message edited by: Vidya Ram ]
 
Sharda R Govindu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vidya,
u r right, if s2 is declared as final, s1 & s3 refer the same object.
Also,Look at this code below:

when i declared s2 as final member of the TestEquals class, s1 and s2 do not refer the same object.
I guess I did not understand some concept here.
Could you please explain.
Thanks
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason being that instance variables of a class even if final are allocated different String object memory locations even if it is the same string... This is because the instance variable is given memory only when the object is instantiated... So te.s2 refers to a different reference than a regular final string that was declared in the same method...
Hence s1 != s3 , but s1.equals(s3) returns true
 
Sharda R Govindu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Preeti,
I guess I'm still confused about object creations in a program.
Pl look at the code below,
the output is : true,true,false,false
1)why is line 3 false? if "A"+"B" and c refer to the same object in memory, then a+b which evaluates to "AB" should refer to the same object, ??

2)are the expressions in println statement evaluated at run time?
3)As Vidya said, 'd=a+b' is evaluated at runtime and therefore a new instance of "AB" is created, so d and c references are not same. Is is not right that "A"+"B" in line1 should also create a new instance of "AB" at runtime and therefore "A"+"B" and c do not refer the same value.

Can someone PLEASE explain this ?
and can someone please guide to me links where I can read more about this topic?
Thankyou.
[ April 28, 2003: Message edited by: Sharda R Govindu ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile Constant Expressions are computed by the compiler. The rest by the JVM.
 
Sharda R Govindu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou!
Sharda
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!