• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Bug in a kathy sierra prog...

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiled run fine here. Can you paste some errors what compiler is complaining about?
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandip,
Have you checked the Environment variables: JAVA_HOME and PATH?
Are they pointing to 1.5 version ?
 
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,
Its working on myside
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sigh.

Here, run this:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic