Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in: String concept.

 
Anish Agrawal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

1. String s="abc";

2. String s1=new String("world");

In SCJP(v 1.4) book by KathySierra chapter no. 6 Java.lang -The Math class,Strings, and Wrappers :

For the statement at line 1 ,its represented diagramatically that a String object with value "abc" gets created on heap and refers s to it.In the statement at line 1 "new String()" is not used then how come String object
gets created on heap.I feel that it just creates the String object in String Constant Pool if compiler doesn't find the literal "abc" in String Constant Pool.Where is this String Constant pool? whether this pool is a part of heap or is totally a different thing and is located somewhere else?

With this confusion my String basic concepts are getting disturbed.Pls explain???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings like anything else, live on the heap. But they are also referenced from a pool of Strings. Check the following article from Javaranch :
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anish,

Get back to me if any doubt.
[ April 25, 2007: Message edited by: Srinivasan thoyyeti ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srini,

I read, Corey's blog in this forum.
It is clearly stated that all the objects in Java are created on the heap and
String are not exceptions. When you write:



An object is created on the heap and the String constant pool stores its
reference. Ref variable s is assigned the reference of the String object
created on the heap.

Next line when we write again like:
2- String s2 = "abc";
The String constant pool is checked whether a reference to String literal
"abc" already exists; if yes the pool returns the reference of the same object just back created on the heap.

That is why when we compare two objects using

(s1==s2) // true

I like if somebody says"
String Constant Pool is pool of String references of objects created on the heap.
Is that correct? Although it composed is my wordings!


Please reply back to me!


Regards,
cmbhatt
[ April 25, 2007: Message edited by: Chandra Bhatt ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

Please go through my second point also.

For new String("xx") -- > There will be two different Objects created( One in pool and One in Heap). Both references are different.
[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah your description speaks loudly!




String constant pool is empty! How many objects the String Const
Pool will be referring to?


Pool is consists of references on the heap or pool itself
consists objects like heap. or WHAT?




Thanks,
cmbhatt
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I can think of:


Two objects are created on the heap. One reference goes to
String Constant Pool and another is assigned to s1 (that is
on the stack).

Correct?


Regards,
cmbhatt
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,


I never mind where pool objects are created !
what i know their references are maintained in pool.

Now i will see whats happenning in back ground !
I am going into String class.
I will notify you when i reach the shore of it.
 
Anish Agrawal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Srinivasan thoyyeti ...It has really helped me to differentiate between the heap and String Constant Pool objects.

But where exactly is this String Constnt pool i.e. where it resides?

One more question is:
String s="hello";
if(s.equals("hi")){
}

in above code when we do the boolean expression evaluation in if
statement then whether an object with value "hi" gets created on
String Constant Pool or not?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

From Language spec:

String literals�or, more generally, strings that are the values of constant expressions (�15.28)�are �interned� so as to share unique instances, using the method String.intern.


So what i understood clearly is :

When ever it encounters a literal( expression) it object created (ofcourse on heap) and interned means pooled.

example:
String str = new String("Hello");

"Hello" literal created on Heap and Interned(means pooled).
This intern will not replace if String already exists in pool.

Now "Hello" Object shallow copied to str.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic