What is Java equals() method?

The Java equals() method is used to compare objects. The equals() returns true if this object is equal to the given object. Otherwise, it returns false.

Belongs to:

The equals() method belongs to the object class. As such, all classes belong to the object class, so equals() method is automatically defined for all classes.

That also means you may compare strings, integers, floats, double or any other objects as using the equals() Java method.

Syntax of using equals()

Following is the syntax of using the equals() method:

public boolean equals(Object obj)

for example, if we have x and y objects then:

x.equals(y)

  • The equals() will return true if x and y are equal.
  • If x is not equal to y then it returns false.

See the following section for examples of using equals method for comparing strings, integers, and other objects.

The example of comparing string using equals()

For this example, a string object is created and assigned a value. The second string object value is assigned by the user input using scanner class.

After that, the Java equals method is used to compare both strings and see the output:

As I entered different strings, following is the result:

In the third result, the difference is ‘c’ and ‘C’ but equals() returned as false. For more on string comparison with equals and other methods visit this tutorial: compare strings in Java.

Note: The String equals() method overrides the object class equals() method.

Comparing Integer objects by Java equals() method

Now, let us look at the usage of equals method for equality check of Integer objects. For that, three Integer objects are created and compared by using the equals() method and the result is displayed:

See online demo and code

The Output:

Java equals

Similarly, you may check the equality of Float, Long, and Double objects. Just like the String equals(), the Integer equals() also overrides the object class equals() method.

Note: As equals() method works with objects, you may not provide primitive type int, float, double etc. variables.

Comparing a String object to Integer example

Sounds interesting? Suppose, your string contains numbers and you want to compare it with numeric objects like Integer, Floats, Double etc. Will equals() method work on that?

The answer is yes. You may compare different type objects for checking the equality. Have a look at this example:

The result:

ObjInt == objC? false

objC == ObjInt? true

ObjInt == ObjStr? true

We may draw the following:

  • Strings can be compared with numeric objects.
  • If string’s number is equal to the numeric object like integer, the result will be false.
  • If you convert a string into the number by using one of the methods like valueOf, and numbers are the same, it returns true.

The example of equals() with Float and Double objects

See the code below where two objects; one Float and the other Double are compared by using equals() method:

The output:

Objflt == Objdbl? false

Using an object with null in equals?

The output will simply be false if an object compared is null as using the equals() method. Otherwise, it will produce a NullPointerException error:

The output:

Java equals null