• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what have you tried?
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:And what have you tried?



Oops, meant to post it!

This is what i tried:

 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic