aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in: String concept. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in: String concept." Watch "Doubt in: String concept." New topic
Author

Doubt in: String concept.

Anish Agrawal
Greenhorn

Joined: Apr 25, 2007
Posts: 3
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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


[My Blog]
All roads lead to JavaRanch
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Anish,

Get back to me if any doubt.
[ April 25, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Apr 25, 2007
Posts: 3
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

Joined: Feb 15, 2007
Posts: 557
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in: String concept.