• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how many Objects created

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above program should we assume 2 Short Objects created or 1 according to being less than 127. in other words, nulling 1 Object (eg o1)
also eligibilze its associated Short?
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ahmed Yehia

It will give you compile time error..
as you can't have something like this

Short s = 15;

As s is a reference type of wrapper class Short
while 15 is an integer(primitive data type)

It's not allowed by compiler..

Regards..
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by khushhal yadav:
Hi Ahmed Yehia

It will give you compile time error..
as you can't have something like this

Short s = 15;

As s is a reference type of wrapper class Short
while 15 is an integer(primitive data type)

It's not allowed by compiler..

Regards..



Well, under jdk 1.4 it gives compile time error,but under jdk 1.5 it compile s fine
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ahmed Yehia:


In the above program should we assume 2 Short Objects created or 1 according to being less than 127. in other words, nulling 1 Object (eg o1)
also eligibilze its associated Short?



For Short only one object and in your code it will not be eligible for GC as o2 also contain it.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B in a similar question, chapter 3 Question #2


When // doStuff is reached, how many objects are eligible for GC?

A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer:
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

could someone illustrate more on this issue
[ June 26, 2007: Message edited by: Ahmed Yehia ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ahmed Yehia:
According to K&B in a similar question, chapter 3 Question #2


When // doStuff is reached, how many objects are eligible for GC?

A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer:
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

could someone illustrate more on this issue

[ June 26, 2007: Message edited by: Ahmed Yehia ]



I did not understand that when the reference to second object by c2 is also made null then it should be also eligible for GC , as it cannot be accessed by any other means. Can somebody explain.
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
may be you guys should see this link : https://coderanch.com/t/261403/java-programmer-SCJP/certification/book-page

And also errata given by Bert Bats
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Mr. Yadav

It is possible to assign Short s = 10; in JDK1.5
just try it on JDK1.5 compiler.
[ June 27, 2007: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ahmed,

you should have a look at the errata, since your example is wrong.

The corrected question:Now there are two objects eligible for garbage collection.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I feel that the two objects eligible for GC are the two CardBoards C1 and C3. The question seems to be interested in knowing about the CardBoards and not Shorts.

Regards,
Aks
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aks Rudra:
I feel that the two objects eligible for GC are the two CardBoards C1 and C3.

c3 had never had a value other than null. No object, nothing to do for the garbage collector.

The question seems to be interested in knowing about the CardBoards and not Shorts.

The question is, how many objects are eligible for garbage collection, and not how many CardBoards.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
The question is, how many objects are eligible for garbage collection, and not how many CardBoards.



C3 is not a object it is reference. SO there is not point of c3 being garbage collected.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if anybody confused why Object refered by c2 is not garbage collected. Refer https://coderanch.com/t/261764/java-programmer-SCJP/certification/PBValue-or-PBRef-Comprehensive-Detail&reply_num=000000&u=00144496 by chander bhatt
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic