I am trying to override equals method in Java. I have a class People
which basically has 2 data fields name
and age
. Now I want to override equals
method so that I can check between 2 People objects.
My code is as follows
public boolean equals(People other){
boolean result;
if((other == null) || (getClass() != other.getClass())){
result = false;
} // end if
else{
People otherPeople = (People)other;
result = name.equals(other.name) && age.equals(other.age);
} // end else
return result;
} // end equals
But when I write age.equals(other.age)
it gives me error as equals method can only compare String and age is Integer.
I used ==
operator as suggested and my problem is solved.
This question is related to
java
overriding
equals
According to Effective Java, Overriding the
equals
method seems simple, but there are many ways to get it wrong, and consequences can be dire. The easiest way to avoid problems is not to override theequals
method, in which case each instance of the class is equal only to itself. This is the right thing to do if any of the following conditions apply:
Each instance of the class is inherently unique. This is true for classes such as Thread that represent active entities rather than values. The equals implementation provided by Object has exactly the right behavior for these classes.
There is no need for the class to provide a “logical equality” test. For example, java.util.regex.Pattern could have overridden equals to check whether two Pattern instances represented exactly the same regular expression, but the designers didn’t think that clients would need or want this functionality. Under these circumstances, the equals implementation inherited from Object is ideal.
A superclass has already overridden equals, and the superclass behavior is appropriate for this class. For example, most Set implementations inherit their equals implementation from AbstractSet, List implementations from AbstractList, and Map implementations from AbstractMap.
The class is private or package-private, and you are certain that its equals method will never be invoked. If you are extremely risk-averse, you can override the equals method to ensure that it isn’t invoked accidentally:
equals
method implements an equivalence relation. It has these properties:Reflexive: For any non-null reference value x
, x.equals(x)
must return true.
Symmetric: For any non-null reference values x
and y
, x.equals(y)
must return true if and only if y.equals(x) returns true.
Transitive: For any non-null reference values x
, y
, z
, if x.equals(y)
returns true
and y.equals(z)
returns true
, then x.equals(z)
must return true
.
Consistent: For any non-null reference values x
and y
, multiple invocations of x.equals(y)
must consistently return true
or consistently return false
, provided no information used in equals comparisons is modified.
For any non-null reference value x
, x.equals(null)
must return false
.
Use the ==
operator to check if the argument is a reference to this object. If so, return true. This is just a performance optimization but one that is worth doing if the comparison is potentially expensive.
Use the instanceof
operator to check if the argument has the correct type. If not, return false. Typically, the correct type is the class in which the method occurs. Occasionally, it is some interface implemented by this class. Use an interface if the class implements an interface that refines the equals contract to permit comparisons across classes that implement the interface. Collection interfaces such as Set, List, Map, and Map.Entry have this property.
Cast the argument to the correct type. Because this cast was preceded by an instanceof test, it is guaranteed to succeed.
For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object. If all these tests succeed, return true; otherwise, return false. If the type in Step 2 is an interface, you must access the argument’s fields via interface methods; if the type is a class, you may be able to access the fields directly, depending on their accessibility.
For primitive fields whose type is not float
or double
, use the ==
operator for comparisons; for object reference fields, call the equals
method recursively; for float
fields, use the static Float.compare(float, float)
method; and for double
fields, use Double.compare(double, double)
. The special treatment of float and double fields is made necessary by the existence of Float.NaN
, -0.0f
and the analogous double values; While you could compare float
and double
fields with the static methods Float.equals
and Double.equals
, this would entail autoboxing on every comparison, which would have poor performance. For array
fields, apply these guidelines to each element. If every element in an array field is significant, use one of the Arrays.equals
methods.
Some object reference fields may legitimately contain null
. To avoid the possibility of a NullPointerException
, check such fields for equality using the static method Objects.equals(Object, Object)
.
// Class with a typical equals method
public final class PhoneNumber {
private final short areaCode, prefix, lineNum;
public PhoneNumber(int areaCode, int prefix, int lineNum) {
this.areaCode = rangeCheck(areaCode, 999, "area code");
this.prefix = rangeCheck(prefix, 999, "prefix");
this.lineNum = rangeCheck(lineNum, 9999, "line num");
}
private static short rangeCheck(int val, int max, String arg) {
if (val < 0 || val > max)
throw new IllegalArgumentException(arg + ": " + val);
return (short) val;
}
@Override public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof PhoneNumber))
return false;
PhoneNumber pn = (PhoneNumber)o;
return pn.lineNum == lineNum && pn.prefix == prefix
&& pn.areaCode == areaCode;
}
... // Remainder omitted
}
record Person ( String name , int age ) {}
if(
new Person( "Carol" , 27 ) // Compiler auto-generates implicitly the constructor.
.equals( // Compiler auto-generates implicitly the `equals` method.
new Person( "Carol" , 42 )
)
) // Returns `false`, as the name matches but the age differs.
{ … }
While your specific problem is solved (using ==
for equality test between int
primitive values), there is an alternative that eliminates the need to write that code.
record
Java 16 brings the record feature.
A record is a brief way to write a class whose main purpose is to transparently and immutably carry data. The compiler implicitly creates the constructor, getters, equals
& hashCode
, and toString
.
equals
method provided automaticallyThe default implicit equals
method compares each and every member field that you declared for the record. The members can be objects or primitives, both types are automatically compared in the default equals
method.
For example, if you have a Person
record carrying two fields, name
& age
, both of those fields are automatically compared to determine equality between a pair of Person
objects.
public record Person ( String name , int age ) {}
Try it.
Person alice = new Person( "Alice" , 23 ) ;
Person alice2 = new Person( "Alice" , 23 ) ;
Person bob = new Person( "Bob" , 19 ) ;
boolean samePerson1 = alice.equals( alice2 ) ; // true.
boolean samePerson2 = alice.equals( bob ) ; // false.
You can override the equals
method on a record, if you want a behavior other than the default. But if you do override equals
, be sure to override hashCode
for consistent logic, as you would for a conventional Java class. And, think twice: Whenever adding methods to a record
, reconsider if a record structure is really appropriate to that problem domain.
Tip: A record
can be defined within another class, and even locally within a method.
Introducing a new method signature that changes the parameter types is called overloading:
public boolean equals(People other){
Here People
is different than Object
.
When a method signature remains the identical to that of its superclass, it is called overriding and the @Override
annotation helps distinguish the two at compile-time:
@Override
public boolean equals(Object other){
Without seeing the actual declaration of age
, it is difficult to say why the error appears.
if age is int you should use == if it is Integer object then you can use equals(). You also need to implement hashcode method if you override equals. Details of the contract is available in the javadoc of Object and also at various pages in web.
When comparing objects in Java, you make a semantic check, comparing the type and identifying state of the objects to:
null
Rules:
a.equals(b) == b.equals(a)
equals()
always yields true
or false
, but never a NullpointerException
, ClassCastException
or any other throwableComparison:
instanceof
for type comparison (which only works as long as there are no subclasses, and violates the symmetry rule when A extends B -> a instanceof b != b instanceof a)
. For your Person
class:
public boolean equals(Object obj) {
// same instance
if (obj == this) {
return true;
}
// null
if (obj == null) {
return false;
}
// type
if (!getClass().equals(obj.getClass())) {
return false;
}
// cast and compare state
Person other = (Person) obj;
return Objects.equals(name, other.name) && Objects.equals(age, other.age);
}
Reusable, generic utility class:
public final class Equals {
private Equals() {
// private constructor, no instances allowed
}
/**
* Convenience equals implementation, does the object equality, null and type checking, and comparison of the identifying state
*
* @param instance object instance (where the equals() is implemented)
* @param other other instance to compare to
* @param stateAccessors stateAccessors for state to compare, optional
* @param <T> instance type
* @return true when equals, false otherwise
*/
public static <T> boolean as(T instance, Object other, Function<? super T, Object>... stateAccessors) {
if (instance == null) {
return other == null;
}
if (instance == other) {
return true;
}
if (other == null) {
return false;
}
if (!instance.getClass().equals(other.getClass())) {
return false;
}
if (stateAccessors == null) {
return true;
}
return Stream.of(stateAccessors).allMatch(s -> Objects.equals(s.apply(instance), s.apply((T) other)));
}
}
For your Person
class, using this utility class:
public boolean equals(Object obj) {
return Equals.as(this, obj, t -> t.name, t -> t.age);
}
@Override
public boolean equals(Object that){
if(this == that) return true;//if both of them points the same address in memory
if(!(that instanceof People)) return false; // if "that" is not a People or a childclass
People thatPeople = (People)that; // than we can cast it to People safely
return this.name.equals(thatPeople.name) && this.age == thatPeople.age;// if they have the same name and same age, then the 2 objects are equal unless they're pointing to different memory adresses
}
I'm not sure of the details as you haven't posted the whole code, but:
hashCode()
as wellequals
method should have Object
, not People
as its argument type. At the moment you are overloading, not overriding, the equals method, which probably isn't what you want, especially given that you check its type later.instanceof
to check it is a People object e.g. if (!(other instanceof People)) { result = false;}
equals
is used for all objects, but not primitives. I think you mean age is an int
(primitive), in which case just use ==
. Note that an Integer (with a capital 'I') is an Object which should be compared with equals.See What issues should be considered when overriding equals and hashCode in Java? for more details.
Here is the solution that I recently used:
public class Test {
public String a;
public long b;
public Date c;
public String d;
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Test)) {
return false;
}
Test testOther = (Test) obj;
return (a != null ? a.equals(testOther.a) : testOther.a == null)
&& (b == testOther.b)
&& (c != null ? c.equals(testOther.c) : testOther.c == null)
&& (d != null ? d.equals(testOther.d) : testOther.d == null);
}
}
Since I'm guessing age
is of type int
:
public boolean equals(Object other){
boolean result;
if((other == null) || (getClass() != other.getClass())){
result = false;
} // end if
else{
People otherPeople = (People)other;
result = name.equals(otherPeople.name) && age == otherPeople.age;
} // end else
return result;
} // end equals
Source: Stackoverflow.com