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 Bug in a kathy sierra prog... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bug in a kathy sierra prog..." Watch "Bug in a kathy sierra prog..." New topic
Author

Bug in a kathy sierra prog...

Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi all,

the below given code is for map related manipulations taken fron kathy sierra book for scjp 1.5 (page 563 to be precise)...am pasting the code here...



When i tryied compiling it ...itr gave me errors.......am bugged.
Please help.

Thanks.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Sandip,

What version is your Java compiler? This program requires Java 1.5 or above, due to its use of generics. There's nothing wrong with the program, so you're probably using an older compiler.


SCJP 5.0
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 232
It compiled run fine here. Can you paste some errors what compiler is complaining about?


SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi All,

I hace checked the version and its java 1.5
Also the installation folder says its 1.5
Additionally i have tried 2 compile it with the command:

javac -source 1.5 #filename.java

And its working fine in my friends compiler.
As requested here are the errors i got:



Please help.
Thanks.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Sandip,

Interesting. This looks like it might be a compiler bug. Perhaps you may want to try updating to the latest Sun JDK 5 distribution, or try a different version if you're already using the latest version.
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Hi,
I tried the same code, its working fine on my pc.

But I have another question regarding this.
With reference to line#5, it states in the book that(taken from pg 564 bottom para):
Why didn't we find the Cat key String? Why did it work to use an instance of Dog as a key, when using an instance of Cat as a key failed?
It's easy to see that Dog overrode equals() and hashCode() while Cat didn't.


1) I tried commenting the equals() and hashCode() used in Dog class, but the code runs and produces the same output.
2) If this code set is used:


instead of this one given in the book:

it produces the expected output (Cat key).

So, I feel with regards to 1) and 2), its really not a MUST for a class to override the equals() and hashCode() for using as apart of the key.
Please comment and explain why this behaviour?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Notice how whenever the error messages talk about String, they say java.lang.String. But when they talk about Object, they just say Object. The should give the fully qualified name, java.lang.Object. The fact that they don't suggests that you have done something extremely evil, and created your own class called Object (with no package). Delete the files Object.java and Object.class from your classpath, and never, ever do this again, as it only leads to confusion.


"I'm not back." - Bill Harding, Twister
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi Kelvin,

I tried installing java 5.0 again without any success at the code.
It still doesnt get compiled and am getting those 7 errors i mentioned earlier.
Kindly guide on how to proceed.

Thanks.
prajal Mehta
Ranch Hand

Joined: Dec 03, 2007
Posts: 66
Originally posted by Vidhya Ramaswamy:
Hi,
I tried the same code, its working fine on my pc.

But I have another question regarding this.
With reference to line#5, it states in the book that(taken from pg 564 bottom para): .........



So, I feel with regards to 1) and 2), its really not a MUST for a class to override the equals() and hashCode() for using as apart of the key.
Please comment and explain why this behaviour?


Hi Vidhya,

I also tried yesterday similar code like yours ..!
I too have the same doubt.
I think you should start a new topic for this doubt, so that we will get some answers!
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi Kelvin,

I tried installing java 5.0 again without any success at the code.
It still doesnt get compiled and am getting those 7 errors i mentioned earlier.
Kindly guide on how to proceed.

Thanks.
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Hi Sandip,
Have you checked the Environment variables: JAVA_HOME and PATH?
Are they pointing to 1.5 version ?
Rizwan Qadri
Ranch Hand

Joined: May 28, 2007
Posts: 40
Hi,
Its working on myside
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sigh.

Here, run this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bug in a kathy sierra prog...