aspose file tools*
The moose likes Beginning Java and the fly likes String immutability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String immutability" Watch "String immutability" New topic
Author

String immutability

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
A String is immutable.When I am using following code, value of static String sName is not changing

Output : good
----------------
Now when I am using name in place of sName in nameTest(), sName output is changing as follows

Output : good idea
---------------------
Why am I getting different output ? WHen does a string become immutable an when can it change vaues ?

Thanks
Rupesh Mhatre
Ranch Hand

Joined: Apr 29, 2011
Posts: 35

String is always immutable.
You are getting confused with local variable and instance variable.
nirjari patel wrote:
public void nameTest(String sName){
sName = sName + " idea ";
//start();
}

In this case variable sName is local variable which does shadow the instance variable sName. As sName is local variable in this case scope of it is limited only for that method and instance vaeiable sName still pointing to "good", hence the result
Output : good
----------------
nirjari patel wrote:
Now when I am using name in place of sName in nameTest(), sName output is changing as follows

public void nameTest(String name){
sName = name + " idea ";
//start();
}

In this case local variable name is name and you are assigning value to instance variable i.e. sName hence the
Output : good idea
---------------------
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

"Immutable" means that you cannot change the content of a String object after it has been created. However, if you have a variable of type String, then ofcourse you can make the variable refer to a different String object.

When you do something like this:

then you are not changing the content of any String object. What happens in line 2 is that a new String object is created, which will contain the content of the first object ("hello") concatenated with the content of the second object (" world").

Note the difference between variables and objects: a variable is a reference to an object - it is not the object itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
1)
In this case variable sName is local variable which does shadow the instance variable sName. As sName is local variable in this case scope of it is limited only for that method and instance vaeiable sName still pointing to "good", hence the result


sName is declared as static, that is one per class. So if I modify sName value in anywhere in the class, will it not be affected ? Thats why I am confused, that when a variable is static, how come the vaue is not affected ?


2)
String s1 = "hello";
s1 = s1 + " world" ;

when we do this, does s1 refer to a new object ? Does it mean that String which s1 referred to earlier (hello) becomes garbage collectible ?

Thanks

Rupesh Mhatre
Ranch Hand

Joined: Apr 29, 2011
Posts: 35

nirjari patel wrote:sName is declared as static, that is one per class. So if I modify sName value in anywhere in the class, will it not be affected ? Thats why I am confused, that when a variable is static, how come the vaue is not affected ?

As already said local variables always shadows instance variables.
public void nameTest(String sName) and in this method definition you are passing parameter as sName which becomes local variable for nameTest method and it does shadow instance variable sName evenif it's a static variable.

nirjari patel wrote:String s1 = "hello";
s1 = s1 + " world" ;
when we do this, does s1 refer to a new object ? Does it mean that String which s1 referred to earlier (hello) becomes garbage collectible ?

yes s1 does refer to new Object and yes String which s1 referred to earlier (hello) becomes eligible to be garbage collected
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
As already said local variables always shadows instance variables

what do you mean by "shadows" in your reply ?
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

Here the term shadows means ...making the local variable sName visible and making the static variable sName as invisible...(ie., local variable sName is covering the static instance variable sName in the class due to scope of the local variable. )

Hope i clarified your doubt...

nirjari patel wrote:what do you mean by "shadows" in your reply ?
Rupesh Mhatre
Ranch Hand

Joined: Apr 29, 2011
Posts: 35

In simple terms local variables get preference over instance variable.
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Rupesh Mhatre wrote:String s1 = "hello";
s1 = s1 + " world" ;
when we do this, does s1 refer to a new object ? Does it mean that String which s1 referred to earlier (hello) becomes garbage collectible ?
yes s1 does refer to new Object and yes String which s1 referred to earlier (hello) becomes eligible to be garbage collected



This is incorrect. ""Hello" doesnt become elligible for garbage collection. It may have aother references to is as it sedies in the String constant pool


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String immutability