wood burning stoves*
The moose likes Java in General and the fly likes Is Kathy&Bert book wrong? Or my understanding wrong? 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 » Java » Java in General
Bookmark "Is Kathy&Bert book wrong? Or my understanding wrong?" Watch "Is Kathy&Bert book wrong? Or my understanding wrong?" New topic
Author

Is Kathy&Bert book wrong? Or my understanding wrong?

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
Hi,

Somebody please explain...

The following is a stuff found in Kathy & Bert book (SCJP1.6) as per one of the members of Javaranch
String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object
in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will
be placed in the pool.


As per the following link, the article in Java ranch says that only one object is created in heap and only the reference is placed in the literal pool
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

Is it that Java1.6 has undergone some change to accomodate an extra object in the pool???
Or is Kathy & Bert book wrong (which is most likely not) [FYI... I am a great fan of Kathy/Bert book. I have read the SCJP 1.2 book which impressed me a lot]

Or Is my understanding of the entire concept (after reading the Ranch article) totally wrong :-S ??? (And I am definitely not ruling that option!!!)

Request somebody to clarify this....

Regards,
Sriram
Mohamed Jeleel
Greenhorn

Joined: Mar 22, 2010
Posts: 18
Hi,

// This will create two objects. One in the heap and one in the pool if no duplicate there.

// This will only create one objects thats in the pool if no duplicate exist.

Nothing wrong in the K&B book or from the link you provided.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Please search; there have been several similar questions in the last two weeks or so.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Hi Sriram,
There was a similar discussion.
http://www.coderanch.com/t/488922/Java-General/java/new-keyword-objects-Java#2198278

It will clarify your doubts.. especially go through this
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

Thanks
Yogi
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
Thanks for the quick reply Mohamed! :-)

What is the String Literal Pool? Most often, I hear people say that it is a collection of String objects. Although that's close, it's not exactly correct. Really, it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.


This was taken from the article provided in the ranch.
From this I infer that objects will NOT be created in the String literal pool!
Now, Please guide me as where I have gone wrong?

Regards,
Sriram
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Are my comments below correct?
Jim ... ...


BEE MBA PMP SCJP-6
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
str1 = str2; // str1 reference in literal pool is unchanged but it may be GC'd


For this also, I have one reference from the same ranch article and here it goes...

Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.


Remaining look fine to me.

Regards,
Sriram
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Hi Jim,
I ran this program and the test returned 'false'.

str4 points to a newly created object 'beta' .

so str2 and str4 and pointing to the same object.

Also In my understanding as Sriram quoted from the article -GC won't happen as there is always a reference from the String literal pool .

Only if we say str4= null ,the object 'beta' referenced by str4 becomes eligible for GC since it does not have reference from the String Literal Pool.
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
boolean test = (str4 == str2); // test will be true


As Yogesh says, this will be false only.
Reason is that whenever a new operator is encountered, a new object is created and existing object wont get referenced!
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
But now, coming back to my original question...
the statement "String s = new String("abc");" will create only one object or two objects?
And where are these objects placed???

Please explain clearly!!!
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Objects are ALWAYS placed on the heap only. Strings are no exception to that.
And regarding the number of objects created ,as per my understanding..only ONE object is created.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You are forgetting the String literals and the String pool.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Is "String Pool " not a collection of references ?
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
You are forgetting the String literals and the String pool.


So, whats the exact answer or solution to this???

Regards,
Sriram
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Kathy&Bert book wrong? Or my understanding wrong?