• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals method  RSS feed

 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I'm having some trouble with the equals method.
In a class Test, i have 4 private variables:

2 integer values: b and c
and
a character d.

I would like for my program to return true is the values b and c and the character d of 2 objects are the same. Otherwise, to return false.

Thank you!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what have you tried?
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:And what have you tried?


Oops, meant to post it!

This is what i tried:

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to override the equals() method in the Object class, then you'll need to use the same signature as the one in Object. That means that your equals() declaration must look exactly like:

Here's some more info in the API docs
http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:If you are trying to override the equals() method in the Object class, then you'll need to use the same signature as the one in Object. That means that your equals() declaration must look exactly like:

Here's some more info in the API docs
http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-



Thank you!
I know this may be a very basic question, but what does overriding a method mean?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haruki jay wrote:I know this may be a very basic question, but what does overriding a method mean?

There are two words used to describe some methods: "overridden" and "overloaded". These are confused all the time.

An overridden method has the same signature as one in a parent's class. In your case your class implicitly inherits from the Object class which has already defined an "equals" method. When you call equals() on one of your objects you don't want the method in Object to be called, you want YOUR method to be called. If you hadn't supplied your own equals method then calling equals() would have caused the parent's equals() method to be called.

you should also use the "@Override" annotation. This doesn't affect the compiled code but it tells the compiler that you really mean to override a parent's method and so if you screw up and have the same name but different signatures the compiler will give you an error. Example:

An overloaded method is a method with the same name and return type but different arguments. Example:
The compiler will complain if two methods have the same name and signature, but if you change the signature it works correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!