In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
  • Always remember to override hashCode if you override equals so as not to "break the contract". As per the API, the result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.
Answer from Hovercraft Full Of Eels on Stack Overflow
🌐
Medium
medium.com › @alxkm › vs-equals-in-java-whats-the-difference-and-when-to-use-which-727f7f39bce7
Java Object Comparison: == vs .equals()-A Complete Guide | by Alex Klimenko | Medium
August 9, 2025 - Java Object Comparison: == vs .equals()-A Complete Guide In Java, comparing objects might look simple at first glance-but there's a subtle yet crucial difference between == and .equals() that every …
Top answer
1 of 16
762

In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
  • Always remember to override hashCode if you override equals so as not to "break the contract". As per the API, the result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.
2 of 16
140

With respect to the String class:

The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.

On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".

As with any explanation, let it sink in.

I hope this clears things up a bit.

🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ String Methods · Compare strings to find out if they are equal: String myStr1 = "Hello"; String myStr2 = "Hello"; String myStr3 = "Another String"; System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal System.out.println(myStr1.equals(myStr3)); // false ·
🌐
Reddit
reddit.com › r/learnjava › help me understand the difference between "==" and ".equals()" in java
r/learnjava on Reddit: Help me understand the difference between "==" and ".equals()" in Java
July 15, 2025 -

I'm currently working on a project that involves comparing strings, but I keep getting stuck on whether to use the "==" operator or the ".equals()" method. From what I've gathered so far, they seem to do the same thing - is it true? Or are there cases where one should be used over the other?

Top answer
1 of 5
49
From what I've gathered so far, they seem to do the same thing - is it true? No. They don't do the same thing. .equals() is a method, so it only applies to objects (reference types). The method compares the content of an object to another. == is an operator - a test for equality on primitive types and for identity (being the same) for objects (reference types). Generally, you should always use .equals with objects - including String values (they also are objects). The exception is if you want to test if two objects (actually their object references) are identical (have the same reference).
2 of 5
38
The difference is a bit hard to explain using strings, so let's use cars instead! Imagine you and your neighbor both own the same car - two different objects but same manufacturer, same color, same number of seats and so on. .equals() will be true because both these cars share the same properties.  However, == will be false because it's two different cars parking in two different parking lots. Let's say you have a daughter and she's learning to drive. For this reason, you allow her to use your car. You set herCar = yourCar. This means herCar == yourCar because both terms/variables ("herCar" and "yourCar") POINT to the exact same car. If you ask someone to point his finger to her car, he'll point at exactly the same car as when you ask him to point at yours. Another example: if someone hits your car with a stone, causing a bruise, this bruise will also exist on your daughter's car because it is the exact same vehicle. However there won't be a bruise on your neighbor's car because yours and his only equal, but they don't ==. Let's go back to strings: .equals() checks whether the strings look the same, have the same length and consist of the same characters in the same order. == checks whether two strings are stored on the same position inside your computer memory. So if you have a string "ExampleString" stored in your memory several times on several positions, == will only be true if you compare variables pointing to the exact same position of the exact string in your computer memory. It will be false otherwise, even if the strings are the same text. This is the case for all objects. For this reason, you should always use equals for objects. --- tl;dr: .equals() means "does it look the same" and can return true even if it's two different objects in your computer memory. == means "is it EXACTLY the same" and can return false even if the objects are 100% equal as long as it's two different objects stored in different positions in your computer memory.
🌐
Medium
medium.com › towardsdev › understanding-and-equals-in-java-1f552daebaba
Understanding == and equals() in Java | by Nakul Mitra
February 4, 2025 - equals() Method - Checks if two objects have the same content or value. ... Understanding the difference between these two is crucial for writing efficient and bug-free Java applications.
🌐
Reddit
reddit.com › r/learnjava › == vs. .equals()??
r/learnjava on Reddit: == vs. .equals()??
July 3, 2020 -

I'm on MOOC.fi and I'm a little confused on why you can't use .equals() when comparing two objects. And on part 5_12.Song programming exercise, I'm confused on why the parameter takes an Object parameter instead of a "Song" type, and then why do we have to typecast the object to compare it?

🌐
Mistermicheels
learning-notes.mistermicheels.com › equals
Equals (Java) | learning-notes
January 31, 2022 - The implementation of this equals method compares objects using their memory locations, meaning that two objects are only considered equal if they actually point to the exact same memory location and are thus really one and the same object.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › java › what is equals() method in java?
What is equals() Method in Java? | Scaler Topics
January 8, 2024 - In Java, the "==" operator and the equals() method are used to test equality between two variables. While "==" compares object memory locations, equals(), defined in the Object class, can be customized for comparing object contents.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Object.html
Object (Java Platform SE 8 )
October 20, 2025 - The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
🌐
DataCamp
datacamp.com › doc › java › equals
Java Array equals()
It returns true if the arrays are equal and false otherwise. ... import java.util.Arrays; public class IntegerArrayEqualsExample { public static void main(String[] args) { int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = {1, 2, 3, 4, 5}; boolean areEqual = Arrays.equals(array1, array2); ...
🌐
Team Treehouse
teamtreehouse.com › community › java-string-equals-string-in-if-statement
Java string equals string in if statement (Example) | Treehouse Community
April 2, 2019 - Getting to know the String class in java took me some time. In general both the equals() method and “==” operator in Java are used to compare objects to check equality but the ‘’==’ operator’ checks if both objects point to the same memory location whereas the equals() method evaluates the values inside the objects.
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-equals-method-in-java
Java String equals() Method - GeeksforGeeks
July 23, 2025 - String equals() method in Java compares the content of two strings. It compares the value's character by character, irrespective of whether two strings are stored in the same memory location.
🌐
Baeldung
baeldung.com › home › java › core java › java equals() and hashcode() contracts
Java equals() and hashCode() Contracts | Baeldung
January 8, 2024 - First, if the object is equal to itself it will return true. Second, if it is not an instance of Money it will return false. Third, we compare it with the attributes of another Money class instance. In detail, we ensure that all attributes of the compared class match those of the comparing class. Java SE defines the contract that our implementation of the equals() method must fulfill.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-equals-compareto-equalsignorecase-and-compare
Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare() - GeeksforGeeks
January 22, 2026 - The equals() method compares the content of two strings. It returns true if both strings have the same sequence of characters. JAVA ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-and-equals-method-in-java
Difference Between == Operator and equals() Method in Java - GeeksforGeeks
January 4, 2025 - The main difference is that string equals() method compares the content equality of two strings while the == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.
🌐
SitePoint
sitepoint.com › blog › java › how to implement java’s equals method correctly
How to Implement Java's equals Method Correctly — SitePoint
November 13, 2024 - But what does “the same value” mean? It is, in fact, the implementation of the equals method in Java that determines “sameness”. The equals method is defined in Object and since all classes inherit from it, all have that method.
Top answer
1 of 4
5

Yes, == with objects is a reference comparison* (checks if the operands are references to the same object), while equals is whatever the class involved defines it to mean (within the requirements documented for equals). Some classes define equals as being the same as ==, including Java's arrays. (Because they don't override the default implementation from Object.equals, which uses ==.)

If you want to compare Java arrays based on the equality of their contents, you use Arrays.equals instead.

Your experiment would have worked if you used a class that defined equals in a useful way, you were just unlucky picking arrays. It's a bit tricky to find a class in the JVM to use for this experiment, because so many either don't implement equals (like arrays) or could be confusing because there are several immutable classes which may reuse instances (although not if you explicitly use new; but I don't want to go down a path of having you use new with something you probably shouldn't, like String; more on that here). I'm going to give up on picking a good example and use the slightly old class SimpleDateFormat:

DateFormat a = new SimpleDateFormat("yyyy-MM-dd");
DateFormat b = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(a == b ? "== Same" : "== Different");
System.out.println(a.equals(b) ? "equals Same" : "equals Different");

That outputs

== Different
equals Same

because SimpleDateFormat defines equals to check that the other object is also a SimpleDateFormat with the same formatting.

Live example


Re your comment on the question:

I have someone the answer points but I only get the == part, if .equals is checking the content, how come the code didnt print "same" for the second if

Because equals doesn't, necessarily, check content. It only does if a class overrides the default Object.equals (which just uses ==) and implements a content check. Arrays don't. (One could argue that they should have, but they don't.) Other classes, like SimpleDateFormat and String and Integer and HashMap do.

In essence: == is always a reference comparison. equals may or may not be a contents comparison, depending on what class you're using it on.

So for instance, say we have this class:

class Example1
{
    private int content;

    Example1(int content) {
        this.content = content;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Example1 a = new Example1(42);
        Example1 b = new Example1(42);
        System.out.println(a == b ? "== Same" : "== Different");
        System.out.println(a.equals(b) ? "equals Same" : "equals Different");
    }
}

Since that class doesn't override equals, you'll get the same answer ("Different") for both == and equals. Live Example.

But if we override equals to define what it would mean for two instances to be equal (in this case: because they have the same content):

class Example2
{
    private int content;

    Example2(int content) {
        this.content = content;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !obj.getClass().equals(this.getClass())) {
            return false;
        }
        Example2 other = (Example2)obj;
        return this.content == other.content;
    }

    @Override
    public int hashCode() {
        return this.content;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Example2 a = new Example2(42);
        Example2 b = new Example2(42);
        System.out.println(a == b ? "== Same" : "== Different");
        System.out.println(a.equals(b) ? "equals Same" : "equals Different");
    }
}

Now equals says two instances with the same content are the same, because the class defines what that means. Live Example. (Also note that when overriding equals, you must override hashCode, which is why I've done so above.)


* More generally, == tests if the values of its operands are the same. The value in the case of reference types is an object reference, and two object reference values are only the same when they refer to the same object are are only different when they refer to different objects (or one of them is null; not referring to an object at all).

2 of 4
0

To create object you need some portion of memory to store it.

Operator == checks if two variables point to the same memory block. That is they points to the same object instance (they are one person under different aliases).

equals() is a method and checks equality by some algorithm. Meaning two object contain the same data (they are different persons but with the same face, twins for example). Default implementation for java.lang.Object#equals is

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

Using you IDE you can ensure that equals for an array is actually java.lang.Object#equals. To check two arrays for equality you should use Arrays.equals. It has also a lot of other useful methods for manipulating with arrays. It is a good idea to check this class first when you need to do something with an array. It saves much time and code lines.

EDITED

Arrays (int[], float[], String[], AnyClass[]) does not implement custom equals method. So for array and for any your custom class equals() and == do same operation unless you override equals() and provide custom comparison. Like

public class Person {
    public String name;
    public String surname;

    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public boolean equals(Object thatObj) {
        if (thatObj instanceof Person) {
            Person that = (Person) thatObj;
            return this.name.equals(that.name) &&
                this.surname.equals(that.surname);
        }
        return false;
    }
}
🌐
Medium
medium.com › @dr4wone › equals-vs-compareto-in-java-understanding-the-differences-fce0a0d4b292
Equals vs. compareTo in Java: Understanding the Differences | by Daniel Baumann | Medium
April 9, 2023 - The equals() method is used to compare objects for equality. It returns true if two objects are equal and false if they are not. By default, the equals() method in Java compares objects based on their memory location.
🌐
Intellipaat
intellipaat.com › home › blog › java object equals() method
Java Object equals() Method - Explained with Examples
August 1, 2025 - String comparison is a fundamental operation in Java, often used in situations like user authentication, database queries, and text processing. While == checks for reference equality and .equals() compares actual content, Java also provides Objects.equals() as an alternative method.