aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection with immutable short. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection with immutable short." Watch "Garbage collection with immutable short." New topic
Author

Garbage collection with immutable short.

jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
hi all from the K&B book chapter 3 question 1 regarding the garbage collection:






if i change the value "Short story = 200" to lower than 127, then what will be the answer? Since the Short value will become immutable, and it will reside back to "pool".
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Shorts are always "immutable". "Immutable" means "unchangeable". What happens when you change the value from 200 to 127 is that the auto-boxed Short is pooled in the cache. So since it's pooled in the cache, it won't be eligible for garbage collection.
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
Kaydell Leavitt wrote:Shorts are always "immutable". "Immutable" means "unchangeable". What happens when you change the value from 200 to 127 is that the auto-boxed Short is pooled in the cache. So since it's pooled in the cache, it won't be eligible for garbage collection.


Shorts is always "immutable" only when the value lay in the range of -128 to 127?




so if the value change to lower than 127 then the answer would be B?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688


Shorts is always "immutable" only when the value lay in the range of -128 to 127?


Definition of Immutable
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
Kaydell Leavitt wrote:

Shorts is always "immutable" only when the value lay in the range of -128 to 127?


Definition of Immutable


ok thanks now i understand the definition, but what is the answer for the question in these 2 cases?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384



I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.


As for the question of this thread has post , C is correct ans. As c1 and cb within go() is eligible for GC. so count is 2




John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.

It is a Short object with an associated value of 200. The value of 200 on the right side is a primitive which is autoboxed. The new object is created in this autobox process. So a new Short object is created even if you don't do

As c1 and cb within go() is eligible for GC. so count is 2

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John
jamil lusa
Ranch Hand

Joined: Aug 18, 2011
Posts: 59
John Stark wrote:
I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.

It is a Short object with an associated value of 200. The value of 200 on the right side is a primitive which is autoboxed. The new object is created in this autobox process. So a new Short object is created even if you don't do

As c1 and cb within go() is eligible for GC. so count is 2

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John


so if the question is modified as the 200 become 100, what will be the possible answer?
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
In that case I would say only one object is eligible for GC.

John
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


Why C1 is not eligible for GC who else is pointing to object created in line 1.




John Stark wrote:

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John


John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Oops, sorry I was only looking at
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection with immutable short.
 
Similar Threads
Objects - GC
How manually check if object is available for garbage collection ?
Garbage collection
Question from mock test
Garbage collection