my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many Objects created 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 "how many Objects created" Watch "how many Objects created" New topic
Author

how many Objects created

ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424


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?
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

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..


rgrds,
Khushhal
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
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


ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
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

Joined: Apr 22, 2006
Posts: 424
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 ]
neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
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.


Neha Verma
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
may be you guys should see this link : http://www.coderanch.com/t/261403/java-programmer-SCJP/certification/book-page

And also errata given by Bert Bats
AtulKumar Gaur
Ranch Hand

Joined: Jun 24, 2007
Posts: 40
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 ]
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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.
Aks Rudra
Greenhorn

Joined: Jun 27, 2007
Posts: 1
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jun 17, 2007
Posts: 37
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.
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
if anybody confused why Object refered by c2 is not garbage collected. Refer http://www.coderanch.com/t/261764/java-programmer-SCJP/certification/PBValue-or-PBRef-Comprehensive-Detail&reply_num=000000&u=00144496 by chander bhatt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how many Objects created