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

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

 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49731
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search; there have been several similar questions in the last two weeks or so.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are my comments below correct?
Jim ... ...
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are forgetting the String literals and the String pool.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "String Pool " not a collection of references ?
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are forgetting the String literals and the String pool.


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

Regards,
Sriram
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic