• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

okay what am i doing wrong?

 
f. nikita thomas
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

silly me, that wasn't the talking end...
 
f. nikita thomas
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob. how do i override hashCode() correctly?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can go through this link also :Use of hashcode and equals
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome And that article Harshit Rastogi posted looks good, too.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic