File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String immutability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String immutability" Watch "String immutability" New topic
Author

String immutability

Mattia Battiston
Greenhorn

Joined: Mar 23, 2009
Posts: 7
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

Joined: Mar 26, 2009
Posts: 131
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.


Programmer Analyst || J2EE web development/design
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
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.


SCJP 5
Mattia Battiston
Greenhorn

Joined: Mar 23, 2009
Posts: 7
Thank you both very much, I've understood now!

Mattia
Gökhan Sakalli
Greenhorn

Joined: Apr 04, 2009
Posts: 20


Output :
s1 and s2 has ...
s1 and s2 points to ....
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63


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

Joined: Dec 29, 2008
Posts: 21
S1 != S2, because they’re different objects in the heap.
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Paolo, you are so clear in understanding. awesome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String immutability