File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Doubt Generics-SelfTest - Indetermination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Doubt Generics-SelfTest - Indetermination" Watch "K&B Doubt Generics-SelfTest - Indetermination" New topic
Author

K&B Doubt Generics-SelfTest - Indetermination

Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Hi everyone.
In K&B, page 614 the following code is given:

and the answer:
As the code stands the output will be 3.

As the hashcode of different objects is not necessarily different, the output could as well be 2, couldn't it?

I am feeling really confused at the moment about this question and two others from the thread-chapter. (If a thread calls the wait-method on an object it holds the lock on or is set to sleep, it still may wake up without explicitly being notified from written code? If so, I am also in doubt with the answers to question 9 and 17 from the threads chapter)
[ May 30, 2007: Message edited by: Sasha Ruehmkorf ]
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Hi sasha,

I am feeling really confused at the moment about this question


There is nothing to confuse in this.

In the first case, as the hascode is not explicitly defined t1, t2 and t3 will go in to three diffrent buckets. Thats the reason why the size is 2.

where as in second case, when the hascode is defined, t1 and t2 will go to one and t3 to other. And this is why the answer is 2.
Ranchers,
Correct me if i am wrong.
[ May 30, 2007: Message edited by: Prasad Tamirisa ]

Regards,
Durga Prasad
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Default hashCode() that you get from the Object class hashCode()
definition, each object will have different hashCode.

So I don't think, the size will come 2. You have commented hashCode()
definition of your class so therefore size is 3. Each object has different
hashCode irrespective of what the object encapsulate ("Monday" by two
objects).


I think your doubt runs around, the hashCode() from the Object class
may return same hashCode() for more than two objects, but it is not like
as I think because it is derived from the physical address of the object
and it will be different for objects created using "new" operator that
is the way to create objects of our class (not talking about wrapper classes).



Thanks,


cmbhatt
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
as the hascode is not explicitly defined t1, t2 and t3 will go go in to three diffrent buckets.

Very likely, but why should this be guaranteed?
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
why should this be guaranteed?


Why because, thas the way it behaves..! See what chandra says.I think this clarifies you better.

Default hashCode() that you get from the Object class hashCode()
definition, each object will have different hashCode.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
I think your doubt runs around, the hashCode() from the Object class
may return same hashCode() for more than two objects,

Exactly
From the API:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.

Maybe this sounds nickpicking, but it really confuses me, as especially regarding GC and Threads I learned: It's not about what is likely to happen, but it's about what is guaranteed. So starting a thread the job of which is to print "one" and then immediately after that starting a thread the job of which is to print "two", the output "onetwo" is not determined. OK, got it. If I try it 1000000 times, the output will always be "onetwo", but that would not be the correct answer in the exam. But if I follow this road, my answers to the 3 mentioned questions above would not match the ones given in K&B...
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
It would be better I you had started the threads doubt in new thread.
Anyways,

You said:

I am also in doubt with the answers to question 9 and 17 from the threads chapter)


Threads-Question 9:
Output will be 1 2 for sure.
3 is not going to be printed as there is nobody to notify args so there will
be wait() forever.

If you think the output could be 2 1, give me the reason. Recall the basics
of flow of the program.

Threads-Question 17:
If your confusion goes around hardy.sleep(1000) that it will make hardy
to take cat nap of 1 second, that would be wrong. (I don't think you didn't get that).

Main concern in this question is laurel.wait() and it makes sense that
that thread can't go ahead from the line where laurel.wait() is written
until laurel is notified.

Thanks,
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
It would be better I you had started the threads doubt in new thread.

I agree, sorry...
My Doubts:
HashCode) Different Objects have different hashcode -> Very likely but in very special cases this could be wrong

Thread Question 9) Why shouldn't the thread spontaneously wake up while in waiting-state. As K&B wrote: "Sometimes the JVM may call notify() for reasons of its own." So output "1 2 3" is an option.

Thread Question 17) Nearly the same. If a spontaneous wakeup happens before the hardy-thread causes the exception, "B" would be printed also. (So my choice was G) for this question) [Edit] -> Forget about this one, explanation was wrong and I am not sure if there could be any interruption waking up laurel, but maybe?
[ May 30, 2007: Message edited by: Sasha Ruehmkorf ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Sasha

"Sometimes the JVM may call notify() for reasons of its own." So output "1 2 3" is an option.


I don't know but this may be an exceptional (I may call it special scenario),
exam is very specific to guaranteed things and we are given number of options
to choose it you had to choose two from them all, you may choose 1 2 3 also
as per your description says that JVM may call notify, but I am not sure about
that.



Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Doubt Generics-SelfTest - Indetermination
 
Similar Threads
Detecting Duplicates in Hashmap
doubt in Ch7 of K & B book
How to write toString method to display name of Object
Hashmap Confusion
HashMap Duplicate Element Problem