• Post Reply Bookmark Topic Watch Topic
  • New Topic

Immutability???  RSS feed

 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
Here is my code,

String str = new String("abc");

My Java reference book says that here two objects are created. One "abc" in pool and String Object (referenced by str)in non-pool memory. Okay fine till this.
My doubt is,

Does the String object internally refer to the String literal "abc" created in the pool?? or The String object referneced by str has it's own copy of "abc" in memory to work with.

Also:
Is it String literals that are immutable or String Objects??
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:

Does the String object internally refer to the String literal "abc" created in the pool?? or The String object referneced by str has it's own copy of "abc" in memory to work with.


Is it matter much to you? That this changes for the programing? I think the String object holds it's own copy of "abc".



Is it String literals that are immutable or String Objects??


When the compiler see String literal it creates String object in 'string pool' if there is no object with the same value in the pool.
[ July 18, 2005: Message edited by: George Bolyuba ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any time the compiler sees a string literal, it creates one in the string pool (although it won't created a duplicate if you have "abc" twice).

any time you see the word "new", a new object is created at runtime.

so, in your example, there are two string-like objects created, one in the pool, and one in 'non-pool' memory.

BOTH are immutable.

Note that you can still point your string reference to a NEW string, but the old one stays there unchanged...
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun I think you need a dose of this article,
Corey McGlone
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All I want to know is this !!

String str = new String("abc");

Now does str refer an object( a wrapper object) which points(!!!) to the literal "abc" created in pool.

Say I write a code like this,

String s1= "abc";

StringBuffer sb1 = new StringBuffer(s1);
sb1.append("def");

System.out.println(s1);
System.out.println(sb1);

Now initially does sb1 refer to a String Buffer object and does that String Buffer object refer to the String Literal "abc".

When we say sb1.append is there a new Literal ("abcdef") created with the "abc"(the one referred by s1) intact.

If it is so,
Then String buffer keeps creating new literals as we keep changing it??

Just wanted to know what actually is happening???
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:

Now does str refer an object( a wrapper object) which points(!!!) to the literal "abc" created in pool.


No. str refer to an String object in the heap and that object doesn't point to anything. It just holds value "abc".

Originally posted by Arun Kumarr:

Now initially does sb1 refer to a String Buffer object and does that String Buffer object refer to the String Literal "abc".


No. the StringBuffer holds value "abc". Once you create StringBuffer it holds a copy of String (the one referred by s1). Calling "append" method you change copy of String object's value. Original String object stays "abc". It's immutable .
[ July 18, 2005: Message edited by: George Bolyuba ]
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crystal.
Thanks Abdullah that was indeed an over-dose.
Thanks George.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!