• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String immutability

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
Hibernate jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
---------------------
 
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
"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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Hibernate jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

As already said local variables always shadows instance variables


what do you mean by "shadows" in your reply ?
 
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
Hibernate jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In simple terms local variables get preference over instance variable.
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic