Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class StringTest
{
public static void main(String args[])
{
String s="java";
s=s+" hi";
System.out.println(s);
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean to the s variable is pointing towards teo objects? "java" and "java hi"
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



is this diagram correct
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this program.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



so will java object be garbage collected
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry, I understood now, thanks for the quick reply.
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup s1 and s2 are the objects...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic