• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

okay what am i doing wrong?

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22265
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

silly me, that wasn't the talking end...
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob. how do i override hashCode() correctly?
 
Marshal
Posts: 73270
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can go through this link also :Use of hashcode and equals
 
Campbell Ritchie
Marshal
Posts: 73270
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome And that article Harshit Rastogi posted looks good, too.
 
Oh the stink of it! Smell my tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic