• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Immutability of strings???

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abhimanyuu abhimanyuu, please check your private messages. You can see them by clicking My Private Messages.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic