wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes IF strings are immutable why is the variable s appending and changing its obect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "IF strings are immutable why is the variable s appending and changing its obect" Watch "IF strings are immutable why is the variable s appending and changing its obect" New topic
Author

IF strings are immutable why is the variable s appending and changing its obect

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

class StringTest
{
public static void main(String args[])
{
String s="java";
s=s+" hi";
System.out.println(s);
}
}


http://www.lifesbizzare.blogspot.com || OCJP:81%
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Remember that a variable is not an object; it points to an object. You can have more than one variable all pointing to the same object.

The expression 's + "hi"' creates a new String, and the variable "s" is set to point to it. The original String object that "s" pointed to is unchanged.


[Jess in Action][AskingGoodQuestions]
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

you mean to the s variable is pointing towards teo objects? "java" and "java hi"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes. First it points to "java" and then it is moved to point to another completely separate String containing "java hi".
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055




is this diagram correct
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

"java" doesn't get destroyed; it's just not referenced anymore. For most objects, that means the garbage collector will eventually delete it. For this particular String, since it's a literal (a String constant that appears in the source code) it's held in the string pool, so it won't be GCd.
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
try this program.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055




so will java object be garbage collected
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
yes
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

If it is no longer necessary, yes. But as Ernest said, that may happen later than regular objects, if the String pool decides to hang onto it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi friends, can anybody tell me why st1 == st4 is false and st1 == str5 is false.

Thank you all in advance. Have a nice day.


There is no rule that we all should know everything. Lets learn few things of everything here.
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
String s4 is a new object...so calling == gives false as they are different objects...since s5 is "change me" + words....this creates a new string in the pool...so s1 and s5 refers to different objects in the string pool...s5 is not a constant because of this


Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi Ted can you be more specific, really I haven't understood.

As you said str1 and str4 are different objects, I got a false. Then str1 and str2 are also different objects, but they are returning true. Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

krishna anusha wrote: Then str1 and str2 are also different objects, but they are returning true. Why?


Because they are not different objects. The strings assigned to str1 and str2 are compile time constants, so the compiler uses the string pool, and hence, they refer to the same string object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi Henry, I understood now, thanks for the quick reply.
Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
yup s1 and s2 are the objects...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IF strings are immutable why is the variable s appending and changing its obect