Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do something with String in non-pool memory

 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know String is immutable because it is in pool memory.

but , String s = new String("abc"); in this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it.

if we do something with s in normal memory, just like this s.replace('a','f'); .in this case, s is just a normal object, why s is immutable ?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you write
String str = new String("javaranch");

this line will create two string object,

at the String constant Pool : "javaranch" Object will be created
at the non String constanct Pool : a normal string Object

so

str --> refer to a String object at non constant pool --> refer to "javaranch" at the String constant pool.

this is as per my understanding.
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ,but I still think str is a String object not a referance of String to constant pool.
 
imaya Munusamy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sunny Jain:
when you write
String str = new String("javaranch");

this line will create two string object,

at the String constant Pool : "javaranch" Object will be created
at the non String constanct Pool : a normal string Object

so

str --> refer to a String object at non constant pool --> refer to "javaranch" at the String constant pool.

this is as per my understanding.


hi sunny i read the same thing in some book but i am confused when i see intern() method of string in api document. it is given as below.

"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned."

if new String add the value in string pool then there is no meaning for otherwise because there will be always literal value in string pool.... can you clarify this
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd highly recommend you review the article Strings, Literally, which discusses the String Literal Pool in detail, and the difference between creating a String using a literal versus using new.

Using new forces the JVM to create a new String object on the heap, as opposed to using an existing object reference from the String Literal Pool.

To verify this, we can write a very simple test program:

 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Crifer:
if we do something with s in normal memory, just like this s.replace('a','f'); .in this case, s is just a normal object, why s is immutable ?


String is immutable.

To test this, try this:



String.replace(char,char) returns a new String reference. the original String stays the same. As you can see if you run the program above, the original String has not changed.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Crifer:
I know String is immutable because it is in pool memory.

That's not really the reason that String objects are immutable - it's really the other way around.

The String pool is an optimization, to avoid having many String objects with the same value in memory. This optimization is possible because String objects are immutable (not the other way around).
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.

I think I got the idea . String is alway immutable not because it is in pool .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"imaya M",
Please check your private messages regarding an important administrative matter.
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic