aspose file tools*
The moose likes Beginning Java and the fly likes okay what am i doing wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "okay what am i doing wrong?" Watch "okay what am i doing wrong?" New topic
Author

okay what am i doing wrong?

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
i'm trying to check the Reference class for equality...


but when i check...


i get this...


shouldn't r0.equals(s) return false? what is the correct way to check for reference equality in a class? any help will be greatly appreciated. thanks in advance.

Imagination is more important than knowledge "Albert Einstein"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Why should r0 and s be unequal? They both have 5 and "james" as their values. Your equals method only uses "i" and "name", it ignores "r".

Also, don't forget to override hashCode(), or you will run into unexpected behaviour when using HashSet or HashMap.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87

silly me, that wasn't the talking end...
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
thanks Rob. how do i override hashCode() correctly?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
There is an article about that in Joshua Bloch's book. Fortunately it (older edition) is available free of charge here. Note that in newer editions the instruction to multiply by 37 has changed to multiply by 31. You can also find details by googling "angelika langer hashcode java" but that only helps if you can read German.
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
and thank you Campbell. i've been looking for something akin to this article for awhile. googling gives such " ",<insert expletive>,results that knowing what is true or correct leaves you at best concerned and at worst implementing bad practices. i'm reading the article now and it's by far the most comprehensible i've seen. again, thank you. take care.
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
you can go through this link also :Use of hashcode and equals


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You're welcome And that article Harshit Rastogi posted looks good, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: okay what am i doing wrong?