This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do something with String in non-pool memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Do something with String in non-pool memory" Watch "Do something with String in non-pool memory" New topic
Author

Do something with String in non-pool memory

Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
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 ?
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
Thank you ,but I still think str is a String object not a referance of String to constant pool.
imaya Munusamy
Greenhorn

Joined: May 19, 2008
Posts: 20
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

Joined: Mar 22, 2008
Posts: 265

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:



There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
Thank you all.

I think I got the idea . String is alway immutable not because it is in pool .
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"imaya M",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do something with String in non-pool memory
 
Similar Threads
doubt regarding String constant pool
Object Create
String
Regd. Strings...
strings