• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 13091
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:

 
Bartender
Posts: 10980
87
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
Bartender
Posts: 10980
87
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.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic