In .NET I can get away with == when comparing Type objects. Can the same operator be used to compare Class objects in Java or should the equals() method always be used?
-
first you need to understand how `==` and `equals` compares two objects and the constraints involved. – mre Feb 11 '13 at 16:48
-
There is only ever one instance for a given class, so `==` should work. However, keep in mind that you can have two instances of the same NAMED class, in two different class loaders. But these are not in any way identical. – Hot Licks Feb 11 '13 at 16:49
-
@DuncanJones -- Yet at least one of the below respondents didn't do much research either. – Hot Licks Feb 11 '13 at 16:51
-
1@BrianRoach This is not a duplicate of the that question. This is specifically for `Class` objects, that question is far more generic. – Brian Feb 11 '13 at 16:58
5 Answers
Class
doesn't override equals
, so it doesn't matter. If you call equals
on class, the default implementation will just do a reference comparison. Here's the default implementation from Object
, the one that Class.equals
calls:
public boolean equals(Object obj) {
return (this == obj);
}
That being said, the only time you'll have two Class
instances of the same class will be when they share their name but were loaded in two different places in code, at which point, they're not necesarily equal. This may happen if you have more than one ClassLoader
, but those classes won't necessarily be equal since it's possible that they may have different byte-code. I would also recommend avoiding the multiple ClassLoader
scenario if you can help it, as this leads to unnecessary complexity in class resolution, and some libraries don't support it. Only code that has to dynamically load classes after the application has started should do this, such as JNLP clients, plugin-based applications, etc.

- 17,079
- 6
- 43
- 66
-
+1 This answer seems the most complete, even against those in the linked possible dupe. – Paul Bellora Feb 11 '13 at 17:03
If the logical question is one of value equality, then I would always use equals. The reason for doing so is that it avoids any need for someone reading the code to check whether == is appropriate in the particular case.
If, as in the case of Class, there is never more than one instance with a given value, the correct implementation of equals for that class is reference equality. That is exactly what Class does, by inheriting the Object equals and hashCode methods.

- 25,849
- 4
- 38
- 75
Instances of Class
are canonicalized, so yes, reference equality should be fine in the case of Class
.

- 54,340
- 18
- 130
- 181
In general, use equals()
.
In Java, equals
roughly means, "Is this object semantically equivalent to that object?". The ==
operator, on the other hand, means "Is this object the same object as that object?" See here for more details.
Edit: Class doesn't override Object.equals(). Still use .equals(), but it is identical to == in this special case.
-
Class objects loaded by different class loaders ARE different and should never compare equal. And Class doesn't override `equals()`. – Hot Licks Feb 11 '13 at 16:55
When you compare two instances using ==, you are actually comparing their memory addresses to see if they are references to the same object.
Assume if you create two object as bellow,
Object ob1 = new Object(); Object ob2 = new Object();
now ob1 == ob2 return false. Because ob1 and ob2 refers to different memory addresses.
but if you assign as bellow ob1 = ob2, then (ob1 == ob2) returns true. Because both refers to the same memory address.
If you compare two objects using equals(), then the JVM checks whether you override the equals method. If not it calls the Object's equals method and return boolean accordingly.
If you override the equals() then the jvm returns the boolean according to the implementation.
You have the Vehicle class as bellow,
public class Vehicle {
private int modelNo;
private String color;
/**
* @return the modelNo
*/
public int getModelNo() {
return modelNo;
}
/**
* @param modelNo
* the modelNo to set
*/
public void setModelNo(int modelNo) {
this.modelNo = modelNo;
}
}
now you create two Vehicle instances as bellow and call the equal()
Vehicle vehicle1 = new Vehicle();
vehicle1.setModelNo(111);
Vehicle vehicle2 = new Vehicle();
vehicle2.setModelNo(111);
System.out.println(vehicle1.equals(vehicle2));
The output should be false, because we dont override the equals() and it calls the Object's equals() and it checks the memory address and returns false.
Now we modify the Vehicle class as bellow,
public class Vehicle {
private int modelNo;
private String color;
/**
* @return the modelNo
*/
public int getModelNo() {
return modelNo;
}
/**
* @param modelNo
* the modelNo to set
*/
public void setModelNo(int modelNo) {
this.modelNo = modelNo;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Vehicle))
return false;
Vehicle other = (Vehicle) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
}
Now we create two Vehicle instances with the same modelNo and invoke the equals() on one of the instances as bellow,
Vehicle vehicle1 = new Vehicle();
vehicle1.setModelNo(111);
Vehicle vehicle2 = new Vehicle();
vehicle2.setModelNo(111);
System.out.println(vehicle1.equals(vehicle2));
Now the output should be true. Because we override the equals method in Vehicle class. So, when we call the equals method on any instance of Vehicle, then the overiden equals() will be called. It compares the modelNo in both Vehicle instances and return the boolean accordingly.
Note: If you override the equals() then thr best practice to override the hashcode() also.

- 29
- 1
- 7