• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String is immutable object but??

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Honk if you love justice! And honk twice for tiny ads!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic