java.lang.Class does not override the equals method from java.lang.Object, which is implemented like this:

public boolean equals(Object obj) {
    return (this == obj);
}

So a == b is the same as a.equals(b) (except if a is null).

Answer from robinst on Stack Overflow
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › OBJ09-J.+Compare+classes+and+not+class+names
OBJ09-J. Compare classes and not class names - SEI CERT Oracle Coding Standard for Java - Confluence
Finally, the comparison is correctly performed on the two class objects. This noncompliant code example compares the names of the class objects of x and y using the equals() method. Again, it is possible that x and y are distinct classes with the same name if they come from different class loaders.
Discussions

Comparing Java objects with different member variables - Software Engineering Stack Exchange
Java has two interfaces that can be used for sorting (Comparator and Comparable) the first compares two objects of the same type (T) the second can be implemented by a class to provide its natural sort order. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
java - Is it clean to place method for comparing 2 objects of same class into that class? - Software Engineering Stack Exchange
The generally-accepted idiom for ... on the Java Object equals() method, which accepts a different object for comparison: ... Indicates whether some other object is "equal to" this one. ... The Single Responsibility Principle says that a class contains the logic pertaining to that class. My take is that colors define what "Same" means, so the logic to test if two colors are ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
java - Comparing two classes by its types or class names - Stack Overflow
If you want to know whether two ... to compare the two classes -- the first option. I can't imagine why you'd want to do this, but if you want to know whether two objects with different concrete types have types with the same fully qualified name, then you could use the second. If you don't understand "concrete types" and "fully qualified names" in the context of Java then you're ... More on stackoverflow.com
🌐 stackoverflow.com
How to compare two Java objects?
By convention, each Java class has an equals method that is inherited from the base Object class. The actual implementation depends on the use case. In your example, you could check that the two objects have the same values for number, color and ball. More on reddit.com
🌐 r/javahelp
8
1
July 12, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-compare-two-objects
Java Program to Compare Two Objects - GeeksforGeeks
July 23, 2025 - Though the values of dog1 and dog2 are the same, equals() method always checks the reference of the two objects i.e if both the objects passed refer to the same object or not and not their values.
🌐
Baeldung
baeldung.com › home › java › core java › comparing objects in java
Comparing Objects in Java | Baeldung
October 10, 2025 - The equals() method of the Objects helper class solves that problem. It takes two arguments and compares them, also handling null values.
Top answer
1 of 5
3

The “different member variables” is irrelevant. It’s an implementation detail. What you need is a set of rules which of two people comes first.

You could for example sort by family name, then given name, then date of birth, and if these are all three equal, take the name of the school, university or company (which will be different member variables) and compare them as strings. If that is equal, you might have student and employee ids, and the student ids might be unique, and the employee ids might be unique, but student and employee ids might be the same. So you could sort then students first ordered by id, followed by employees sorted by id, if you might sort by if first if student and employee ids are comparable.

(University or school and employer might be the same, because universities are also employers).

2 of 5
3

Comparing objects with different fields sounds like bad polymorphic design, whether it's Java or any other OOP language:

  • If your comparator needs to know the precise subtype of an object to do the comparison, you mess-up with the the open-closed principle, since for every new subclassing, you'd potentially need to modify the comparator to select the relevant fields.
  • If your comparator needs uses reflexion to find on its own the relevant fields to compare, you indirectly mess up with the principle of encapsulation, since you create a hidden requirement that information to be compared must be in some predetermined field.

If you want to sort People properly in a clean polymorphic design:

  • you need to rely either on a field, available for any kind of People, including Student, or
  • you may call some function/transformation that provides a unique value (e.g. a string) that allows to sort any People. People and Student may then just use a different transformation that will be passed to the comparator; Or
  • you only sort among homogeneous subtypes.
🌐
Coderanch
coderanch.com › t › 392768 › java › Comparing-instances-classes
Comparing instances of two different classes - HOW? (Beginning Java forum at Coderanch)
November 2, 2002 - I'd perhaps prefer to write a seperate method that clearly just determines if these two different objects have some similar properties that would determine their equality. I'd probably also prefer that method to be in the data structure that tracks these Customer objects (and not be a behavior of a Customer or Employee). [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio] ... Hi mark, u can check as name that time this will work class Customer { String name; } class Employee2 { String name; double salary; public boolean equals (Object o) { if (!(o instanceof Employee2)) return f
Find elsewhere
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Comparing Java objects with equals() and hashcode() | InfoWorld
May 16, 2024 - In the first comparison, equals() compares the current object instance with the object that was passed. If the two objects have the same values, equals() returns true. In the second comparison, equals() checks to see whether the passed object ...
🌐
Msgprogramator
msgprogramator.sk › home › java › how to compare java objects correctly
Java Objects: How to Compare Them Correctly - msgprogramator.sk
July 7, 2025 - The equality of a variable is defined by the value it refers to. If two variables refer to the same value, they are equal. In Java, this is checked using the equals() method. It is part of the Object class, which is ...
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Comparing_Objects
Comparing Objects - Wikibooks, open books for an open world
However, Java needs to know the comparison rules between two objects. So when you define a new class and want the objects of your class to be sortable, you have to implement the Comparable and redefine the compareTo(Object obj) method.
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit3-If-Statements › topic-3-7-comparing-objects.html
3.7. Comparing Objects — CS Java
Only use == with primitive types like int or to test if two strings (or objects) refer to the same object. Use equals, not ==, with strings which will check to see if they are equal letter by letter. The one common place to use == or != with objects is to compare them to null to see if they ...
🌐
Javatpoint
javatpoint.com › how-to-compare-two-objects-in-java
How to Compare Two Objects in Java - Javatpoint
How to Compare Two Objects in Java - How to Compare Two Objects in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, inheritance, array, string, map, math, methods, examples etc.
🌐
Reddit
reddit.com › r/javahelp › how to compare two java objects?
r/javahelp on Reddit: How to compare two Java objects?
July 12, 2017 -

Hi there, it's my first time learning Java and I'd love some assistance with comparing two Java objects. Here is a KenoBall class:

public class KenoBall {

    private static int number;
    private static String colour;
    private static String ball;

    public KenoBall(int number, String colour) {
        this.number = number;
        this.colour = colour;
        this.ball = number + colour;
        System.out.print(number + colour + "\n");
    } 

    public boolean matches(KenoBall other) {
        if (What goes in here?) {
            System.out.println("Tru");
            return true;
        } else {
            System.out.println("False");
            return false;
        }
    } 

 }

And in my main method:

    public static void main(String[] args) {
        KenoBall k1 = new KenoBall(1, "R");
        KenoBall k2 = new KenoBall(1, "R");
        KenoBall k3 = new KenoBall(4, "B");
    
        k1.matches(k2);
    }

If anyone can point me in the right direction I'd really appreciate it! Basically I'm trying to see if k1 is equal to k2 (and it should).

Thank you!

🌐
Lean Tactic Reference
course.ccs.neu.edu › cs5004 › lectureequality_and_comparison.html
Lecture 6: Equality and comparison
Java facilitates this through the Comparator<T> interface. If two objects of class X must be compared, we can write a comparator as public class XComparator implements Comparator<X>. This object must now contain a method public int compare(X a, X b) that compares two objects of type X.
🌐
InformIT
informit.com › articles › article.aspx
Comparing Object Values and Classes | Working with Objects in Java | InformIT
To see whether two String objects have matching values, a method of the class called equals() is used. The method tests each character in the string and returns true if the two strings have the same value. The EqualsTester application shown in Listing 3.5 illustrates this.
🌐
Sololearn
sololearn.com › en › Discuss › 457108 › how-to-compare-2-objects-of-same-class-in-java
How to compare 2 objects of same class in java? | Sololearn: Learn to code for FREE!
June 10, 2017 - class Account { public: double balance; String name; } Account first = Account(); first.name = "Hemant"; first.balance = 100000.0; Account second = Account(); second.name = "Hemant"; second.balance = 100000.0; I wanna check both fisrt ans second object containts the same data. How to do that? javaif-statementselse-statementsequalscompare · 10th Jun 2017, 1:03 PM · Hemant Makar · 7 Answers · Answer · + 4 · see this https://stackoverflow.com/questions/16069106/how-to-compare-two-java-objects ·