Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

So,.. What does the == operator do, *exactly?  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day comrade programmers.
As my expertise in the Java Programming Language advances, I'm studying and trying to understand new things(for me).

Let's talk about overriding the method hashCode()
I've overriden this method for a class I made, and made it return the int value of one of my instance variables.

Question: Does doing that mean that two objects of the class I made with the same value in the instance variable returned by method: hashCode() will be equal when I use the == operator?

Or will the == operator always use the bits contained in the reference variable even when hashCode() has been overriden?

PD. I suppose I could have just asked: Does overriding hashCode() affect how the == operator works?

Thank you very much in advance as always,
Your Friend,
Jose
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
I suppose I could have just asked: Does overriding hashCode() affect how the == operator works?

No it doesn't. == will always look at the exact object the reference point to, hashCode() has nothing to do with it.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdi, Rob.

Thank you. Your assistance is invaluable. It's so good to be able to receive help from someone who always responds confidently; makes it so much easier for me to digest a concept and move on to next one.

Have a Nice Day.

If someone has some further explanation, please let me know,
Best regards,
Jose
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes Mr.Augustine, it is there. thank you.
I'm glad you gave me that link, I need to do all the reading I can about the intricacies of Java.

Take care,
Jose
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
...I need to do all the reading I can about the intricacies of Java...

In that case, see JLS 15.21 Equality Operators and its subtopics.
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,

I had a very long response typed.. but I noticed you asked for reading material - so I'll point you straight to the source:

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

Probably one of the best books on Java.. take a look at that.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Adam, I'm overwhelmed by your response... and it's true, I need to do all the reading I can. Thanks for providing me the location of that online Book. I sure know it is the best book if you recommend it.

Once more, I gotta say this, you Guys rock! Java Ranch is the greatest online community of all time.

Thank you Mr. Adam.

See you next time.
Sincerely,
Jose
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!