• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String immutability

 
Mattia Battiston
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm studing the String chapter and written some code that is not giving the result I expect. What I understood is that when you create a String, if it already present in the pool then that is assigned to your reference.
This is my test code:



I expected s1==s2 to return true, because both the Strings contains "abcdef" so I thought the JVM assigned s1 and s2 to the same String in the pool, but this doesn't seem correct. This is the output of the execution:
s1 = abcdef
s2 = abcdef
s1 and s2 has the same value
shouldn't print this


Can someone help me clarifying this point?

Thanks a lot folks!
Mattia
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look, the way you expected will work if
instead of setting s1 to "abcdef" using concatenation. you would have done
String s1 = "abcdef";

So when you used the concatenation way it was not a literal (constant String) - remember the pool is constant's pool.

hope this helps.
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mattia Battiston wrote:
Can someone help me clarifying this point?


Hope so! java.lang.String says:


The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method.


String instances are stored in the pool, but StringBuilder and StringBuffer are string-like classes, not String.
 
Mattia Battiston
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both very much, I've understood now!

Mattia
 
Gökhan Sakalli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output :
s1 and s2 has ...
s1 and s2 points to ....
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


String concatenation results in a reference to an object of type String, which is a newly created object if the right-hand side expression is not a compile time constant expression.

The right-hand side of assignment in line 1 is not a compile time expression, so the (newly created) object that s3 refers is not the same that s1 refers (the one in the string pool). For this reason s1 == s3 is false, while s1 == s2 is true.

Hope this amends my previous answer, if not... String Concatenation Operator +

 
Rodrigo Tassini
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S1 != S2, because they’re different objects in the heap.
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paolo, you are so clear in understanding. awesome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic