aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String is immutable object but?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String is immutable object but??" Watch "String is immutable object but??" New topic
Author

String is immutable object but??

mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
public class Stngcheck {

/**
* @param args
*/
public static void main(String[] args) {
Stngcheck ss = new Stngcheck();
String s1="1";
String s2="2";
s2=s1;
System.out.println(s2);
ss.go(s2);

}

public void go(String s3){

System.out.println(s3);
}
}


Hi Javaranchers

since string is immutable object why does s2 prints 1 i know this is pretty silly but i want to know pleaseeeeee
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
s1 = "1"; // created a string object in heap with "1", s1 point to that object
s2 = "2"; // created another string object in heap with "2", s2 point to that object

s2 = s1; // s2 now refer to the object that is referred by s1, which is "1"

ss.go(s2); // passing the reference of s2 to go()

// go() receive a copy of s2 and store in s3, so s3 is reference of "1"
// so you see output "1" from go()
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
but then where does immutabilty comes in picture in KB book i saw e.g like

s.concat without assigning doesnt changes the value otherwise if you
do like s= s.concat("abc");

then value changes
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
Originally posted by mukki pandey:
public class Stngcheck {

/**
* @param args
*/
public static void main(String[] args) {
Stngcheck ss = new Stngcheck();
String s1="1";
String s2="2";
s2=s1;
System.out.println(s2);
ss.go(s2);


}

public void go(String s3){

System.out.println(s3);
}
}


Hi Javaranchers

since string is immutable object why does s2 prints 1 i know this is pretty silly but i want to know pleaseeeeee





Hi,
One important thing we need to remember is String objects are immutable;String references are not.

In the above example,
s2=s1;
s2 reference is changed to point to another object which s1 points to("1").The object which s2 used to refer to "2" is still there but not reachable(abandoned).So the String objects are as such immutable.

It will be more clear if you could just draw a picture.
This pictorial representation is very helpful for solving gc problems too.Trust me.When I started learning this concept,I just tried to solve mentally but couldnt get it.Thats when I started using diagrams.

Moreover the String immutability is also shown by pictorial representation in K&B book.Please go over this chapter once again.I am sure You can get it.

O..another thing.No question is silly.I have asked sillier questions before.Always there will be a patient someone to answer these questions.so dont worry.

hope this helps.
[ October 18, 2008: Message edited by: sumi rankan ]
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
Originally posted by mukki pandey:
but then where does immutabilty comes in picture in KB book i saw e.g like

s.concat without assigning doesnt changes the value otherwise if you
do like s= s.concat("abc");

then value changes


The same thing here.
when you say
String s="abc";
s.concat("def"); // s is not changed by this.

System.out.println(s); //prints abc

s=s.concat("def"); // now "abc" is abandoned and s now refers to abcdef

System.out.println(s); //prints abcdef


So here basically the string reference is changed(not the object)to refer to the newly created object.
2 objects ,abc and abcdef are created.
Hope this clears your doubt.
 
Consider Paul's rocket mass heater.
 
subject: String is immutable object but??