• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Kathy Sierra - SCJP 1.6 - Chapter 3 - Self Test - question 1 - doubt

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering this question taken from Kathy Sierra SCPJ 1.6 book:


The answer in the book is:

C (which means 2 objects) is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.



I would like to know why the Short wrapper object in c1 is elegible if c2.story still refers to the same Short wrapper object.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, my post was incorrect. I was running a test in another screen and hit submit before I tested it. I was wrong :S.
 
joao massan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any idea?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two autoboxed Shorts will be the same instance if the boxed value is between -128 and 127. I don't have the book with me, but if I'm not mistaken, the example from K&B boxes a larger value (200), so the Short instances are not the same. (See this thread in the SCJP forum.)
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's curious. Is the commented out print line in the book, or is it your own addition? Normally, instance variables like story would not be shared among instances, so when the containing object was eligible for garbage collection, then story would be as well. However, the compiler keeps a pool of small integers so instead of constructing them, it just points to the object already in the pool. That's safe because the wrapper classes are immutable. That's why you see the two "story" variables as being equal. Actually, those objects will never be garbage collected even if your program no longer has any references to them. They just stay in the pool ready to be reused.

I think the author might have missed that optimization when she said that there would be a Short eligible for garbage collection. Another distinct possibility is that I'm missing something.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

joao massan wrote:Considering this question taken from Kathy Sierra SCPJ 1.6 book:

I would like to know why the Short wrapper object in c1 is elegible if c2.story still refers to the same Short wrapper object.



c2.story is a completely different object than c1.story they don't point to the same object at all.
your test of == returns true because the Short objects are unboxed into primitives, where the == operator works differently.

EDIT:
I see from the above link that this is wrong, but I'll leave it here as a record of my confusion
 
joao massan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello people...
First of all, thank you for your attention!
I think I found the answer for that problem. It could have been a problem during the translation (I'm using a pt-BR translation of that book). I was taking a look at original (english) version of that book and the value of "story" is 200. With story = 200, c1.story == c2.story returns false.

Thank you.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I added

as an instance variable to Cardboard
the test (before the instantiation of c3)

said "true"

why is that?
 
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you create Strings this way:

they are treated as literals and are put in a string pool and reused - a similar thing to the Short reusing.

however, if you create a String using the new operator, or concatenation - a brand new object will be created and not put in the pool. the pool is for literals only.
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends..

I have a doubt about how many objects eligible for GC..

Assume that the code changed as below :


in that code, How many objects eligible for GC?..
is it 5 objects?..

Thanks..
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say it's 4 objects - c3 and c1 are nulled out, so they are eligible, and two Strings created by new withing them - "String 2", twice.

Strings from the pool are never eligible, and small Shorts and Integers are reused, c2 is still using them. c2 is not eligible, cause it was nulled withing a method, so it means nothing.

why did you say 5?

i might be wrong, gc is the thing i disliked most when i was learning for my scjp
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What objects that you specify eligible for GC?..
are those :


1. c3
2. c1
3. new String("String 2")
4. new String("String 2")



i thought that should be 5 objects eligible for GC, because c1 is assigned to null value..
c1 itself refer to CardBoard which has 4 instances variable : ..


1. Short story = 5;
2. String s1="String 1";
3. String s2=new String("String 2");
4. Integer int1 = 10;



so i thought number of objects eligible for GC is 5, namely :


1. c1
2. story
3. s1
4. s2
5. int1




if that class instantiated, how many objects created?.. is it 2?..
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int1 is not eligible, it's reused just like Short objects.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if the number of objects created is 2 while we instantiate CardBoard..


with that code, the number of objects eligible for GC is exactly only 2?..

please tell me the correct answer..

Thanks in advance..
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i don't know when those wrapper objects are created, probably when it's the first time you use them, so in this case - first instantiation of CardBoard creates 4 objects - CardBoard itself, a Short, an Integer and a String - s2. When a second CardBoard is instantiated - then it's CardBoard and s2 that are created.

so it's definitely not 2 objects eligible for gc, but at least 4 - as i said before - two CardBoards and two Strings.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maciej Drodel wrote:well i don't know when those wrapper objects are created, probably when it's the first time you use them, so in this case - first instantiation of CardBoard creates 4 objects - CardBoard itself, a Short, an Integer and a String - s2. When a second CardBoard is instantiated - then it's CardBoard and s2 that are created.

so it's definitely not 2 objects eligible for gc, but at least 4 - as i said before - two CardBoards and two Strings.




Well, i'm still not understand..


in this case - first instantiation of CardBoard creates 4 objects - CardBoard itself, a Short, an Integer and a String - s2. When a second CardBoard is instantiated - then it's CardBoard and s2 that are created.



Each instantiation of CardBoard referred with different reference, would that be 4 objects created of each instantiation (c1,c2)?..


Sorry, i'm still not clear..
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then you should read a bit more about string pool and wrapper objects reusal, cause i'm out of ideas how to explain that to you.

good luck with your scjp
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you create the first instance you want to create 5 objects c1, story, s1, s2 and int1.
but story and int1 ranges between -127 to 128 so they will point to these values in the constant pool so no new objects created.
s1 & s2 are assigned a literal value which will be added to string constant pool.
so, first time 3 new objects created c1, s1,s2.

now when you create a second instance c2. again 5 objects are to be created namely c2,story, s1, s2 and int1.
again story and int1 will refer to values from constant pool so no new object created.
now s1 & s2 will also refer to the literals creted in the constant pool when c1 was created so they also dont create any new object.

so, story,s1,s2 and int1 variables of both instances c1 & c2 refer to same values in the constant pool.

now c1 is nulled but c2 is not.
so, all the four objects are still referenced by variables in c2 moreover they are in constant pools, so they will not be gced even after the program ends.

with this explaination I think the answer is only 1 object(referred by c1) is eligible for gc.
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:
so, story,s1,s2 and int1 variables of both instances c1 & c2 refer to same values in the constant pool.

now c1 is nulled but c2 is not.
so, all the four objects are still referenced by variables in c2 moreover they are in constant pools, so they will not be gced even after the program ends.

with this explaination I think the answer is only 1 object(referred by c1) is eligible for gc.


first, you forgot about c3.

second, strings created by new do not go to the pool, so they are eligible for gc - so even without c3 in action, there are two objects eligible - c1 and the string.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of notes here:

The question was fixed a LONG time ago to read:

Short story = 200;

Also, be careful when you add code to mock questions - in this case adding String objects is a no-no.

In both of the above cases the key thing to remember is that the exam does NOT test you on your knowledge of any constant pools. So, for instance, there are no questions on the real exam that use objects of type String in a GC question.

hth,

Bert
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replies..

Short story = 200;

Is it mean that the new object won't be created if the value 200?..
The Short itself wrapper class of primitive data type which has range values between -32768 and 32767..

if i set the value of short : Short story2=32767, will it create new object?
 
Maciej Drozdzowski
Ranch Hand
Posts: 40
Google Web Toolkit Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course it will be created.

but every time you do a brand new object will be created, cause 200 is not within the interval [-128,127] and will not be reused.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic