This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constant Pool and Non-Pool strings

 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that strings have a special area called String Constant Pool set aside from them. Hence, if you have

String s1 = "Hello";
Hello is placed in the pool. If i again need a String s2 as

String s2 = "Hello";
It is detected that Hello already exists in the pool and s2 points to the same object. This is done for the conservation for memory.

Now my question is this: If i say

String s3 = new String("Hello");

s3 will not point to the "Hello" in constant pool, instead it will be newly allocated in the non-pool memory and a new entry "Hello" (if it doesen't exist already in pool) will be added. Why is the aforementioned principle of memory conservation not applied here?
[ May 03, 2006: Message edited by: Aniket Patil ]
 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b'coz here we are creating a object of String class....and all the object are stored in Heap.
 
Alberto Cancello
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 String s1 = "Hello";
2 String s2 = "Hello";
3 String s3 = new String("Hello");


There are:

a) In the String pool, one string "Hello". It is referenced by s1 and s2. Also a reference to it is passed to the constructor of the String in line 3.

b) An object String in the Heap, created at line 3. s3 referred to it.

Bye,
Alberto
 
gurpreet singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 String s1 = "Hello";
2 String s2 = "Hello";
3 String s3 = new String("Hello");
4 String s4 = new String("Hello");

what happens with s4?
Will it point to s3 or new object will be created
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will create a new string for s4.
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the string objects are placed only on the heap,even the String literals.
When you say

One object is created in the heap, and an entry is made in String pool to refer to the string object created in the pool.
Now there are two references to the "Hello" object,
*)in reference variable str1
*)in String pool
So there are really no object created in the String pool ever, only the references to the String literals in the heap that are known at the compile time are stored in the String pool.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic