This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Immutability of strings??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutability of strings???" Watch "Immutability of strings???" New topic
Author

Immutability of strings???

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
String s1 = "S1";
String s2 = "S1";

System.out.print("," + (s1==s2));//returns true.

but then when i assign, s1="S2";

then the answer is false.




But how is that possible, here s1 and s2 are pointing to the same String objects(S1), so any change made to s1 should affect the value of s2 as weel, but that is not the case, why???
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

it works like this brother
when you are creating a new string in your program the compiler will check if there is alredy any string with the same name if yes the newly created string will refer to the already created.
Butttttt. once you change content of any of the strings a new string object will be created your reference will point to the newly created object.

thanks,
Tanu Gulati


thanks
Tanu Gulati
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

abhimanyuu abhimanyuu, please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
praveena reddyk
Greenhorn

Joined: Sep 25, 2008
Posts: 11
String s1="s1";
if you are using a String literal then "s1" will be placed in a String constant pool.
if you assign same value with a new fererence i.e String s2="s1";
it checks to find if there is already "s1" in string constant
pool if it is there it will adjust s2 to that now both s1 and s2
are pointing to the same value "s1". so s1==s2 gives true.

But if you write like this s1="s2"; then if there is no value "s2" in String constant pool it will create a new one(i.e "s2") and adjust the reference s1 to "s2" now s1 is pointing to "s2" and s2 is pointing to "s1" and output is s1==s2 is false.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

When you assign a new value to s1, you are not changing the String object that s1 was pointing to; you are only making s1 point to a different String object. s2 will still be pointing to the first String object.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
Any time you refer to identical String literals, then the jvm will return a reference to the same String object.
The question that you have is really nothing to do with immutability of strings.

Let me explain in a little bit more detail.

String s1 = "S1"; //s1 is a reference to the String "S1"
String s2 = "S1"; //s2 is a reference to the same String "S1"

s1 = "S2"; //s1 is now a reference to the String "S2" but s2 is still a reference to the String "S1".

All you have done is assign s1 to a different object. You have NOT changed the value of the object referenced by s2. This will work the same way for all objects, regardless of whether they are immutable or not.

The way to change the value of a mutable object is to perform an operation on it:
MyObject o = new MyObject();
o.changeMe();
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
something else worth adding. It is not normally good practice to use double equals comparison on Strings. Instead use the .equals(Object o) method instead.
The same holds true for all java objects.
[ October 06, 2008: Message edited by: Paul Beckett ]
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks a lot,but i still have a doubt.

class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");
System.out.print(c.equals(d));
}}

the answer is: false,false,true.

But according to what you guys have explained, a+b=AB, (a+b)==(a+b) should be true???
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
String literals are returned from the string pool.
However, most of the time when Strings are created (concatenation,toString(), etc) then the result is not returned from the constant pool.

To explain this a bit more, for your line:
String c = a+b;
the compiler will implement string concatenation approximately as follows:
String c = new StringBuffer().append(a).append(b).toString();

If you really want to use the string constant pool then you can use the intern method. I am NOT reccommending that you do it this way but you can implement as follows:
String c = (a+b).intern();
String d = (a+b).intern();
System.out.println(c==d); //should print true
System.out.println(c.equals(d)); //also true

The best way to check that 2 objects are meaningfully equal use the .equals method.
 
jQuery in Action, 2nd edition
 
subject: Immutability of strings???
 
Similar Threads
How many String Object are created in the following code ??
On String.
How many Object java will create
String immutability
Strings