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
🌐
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.
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.

Discussions

Java: == vs .equals
So this code returns false true true true I understand that “string1” is instantiated as a string object. “string2” is instantiated as an occurrence of the “getString()” method that returns the string “Hello”, and that these two values have different locations in memory · The ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
October 26, 2022
Difference between equals() method and == operator
Hi All, This is a serious issue am facing... I need some good answers for this. I know the difference between == operator (which compares references) and equals() method (which compares the instan... More on forums.oracle.com
🌐 forums.oracle.com
October 12, 2007
== vs. .equals()??
== will just check if the two arguments have the same memory reference while an object's equals() class method checks some internal value to determine equivalence (when you create your own classes, you may implement a custom equals() method). public static void main(String []args){ Object a = new Object(); Object b = a; Object c = new Object(); System.out.println("a == b ? "+ (a == b) ); //true System.out.println("a == c ? "+ (a == c) ); //false System.out.println("a.equals(b) ? "+ a.equals(b)); //true } Every object is a subtype of Object, so (generally) you can pass a Song where the documentation expects Object. More on reddit.com
🌐 r/learnjava
24
32
July 3, 2020
Difference Between equals() and == in Java
Select Topic Area Question Body Explain the difference between the equals() method and the == operator in Java. In which situations should each be used, and what issues can arise from using them in... More on github.com
🌐 github.com
3
2
🌐
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 › @AlexanderObregon › understanding-the-difference-between-equals-and-in-java-10a075326720
Understanding the Difference Between equals() and == in Java
April 24, 2024 - In the example above, str1 and str2 are different objects with the same content, so str1 == str2 returns false. However, str1 and str3 are the same object reference, so str1 == str3 returns true. The equals() method in Java is used to compare the content of two objects.
🌐
Baeldung
baeldung.com › home › java › core java › difference between == and equals() in java
Difference Between == and equals() in Java | Baeldung
November 27, 2025 - New String objects created with the constructor don’t use the pool, so they produce different results with reference checks. Value equality applies when two separate objects contain the same values or state. This comparison uses the equals() method from Object and focuses on the values stored inside the object rather than the reference.
🌐
LinkedIn
linkedin.com › pulse › difference-between-equals-java-babar-shahzad
Difference Between == and equals() in Java
March 29, 2023 - So, the main difference between "==" and "equals" in Java is that "==" compares the memory location of two objects, while "equals" compares the contents of two objects.
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Variables Print Variables Multiple Variables Identifiers Constants (Final) Real-Life Examples Code Challenge Java Data Types · Data Types Numbers Booleans Characters Real-Life Example Non-primitive Types The var Keyword Code Challenge Java Type Casting Java Operators · Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings · Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans
Find elsewhere
🌐
BYJUS
byjus.com › gate › difference-between-operator-and-equals-method-in-java
Difference between == and .equals() method in Java
March 29, 2023 - The major difference between the == operator and .equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.
🌐
Oracle
forums.oracle.com › ords › apexds › post › difference-between-equals-method-and-operator-2764
Difference between equals() method and == operator
October 12, 2007 - Hi All, This is a serious issue am facing... I need some good answers for this. I know the difference between == operator (which compares references) and equals() method (which compares the instan...
🌐
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?

🌐
GitHub
github.com › orgs › community › discussions › 163975
Difference Between equals() and == in Java · community · Discussion #163975
There was an error while loading. Please reload this page. ... In Java, == is an operator that compares object references, meaning it checks whether two variables point to the same memory address.
🌐
Quora
quora.com › Discuss-the-differences-between-the-operator-and-the-equals-method-in-Java-When-would-you-use-one-over-the-other
Discuss the differences between the == operator and the equals() method in Java. When would you use one over the other? - Quora
Answer (1 of 3): In Java, the `==` operator and the `equals()` method serve different purposes when comparing objects. 1. `==` operator: - The `==` operator in Java is used for comparing the references of objects, not their actual content. - When used with objects, it checks whether two object...
🌐
Quora
quora.com › What-is-the-difference-between-and-equals-when-comparing-objects-in-Java
What is the difference between '==' and '.equals()' when comparing objects in Java? - Quora
Answer: Hey, The main difference between “==” and “.equals()” is that the former checks if both variables used are pointing to same memory location or not, and the latter basically compares both the arguments passed through it and doesn't ...
🌐
Blogger
javarevisited.blogspot.com › 2012 › 12 › difference-between-equals-method-and-equality-operator-java.html
Difference between equals method and "==" operator in Java - Interview Question
It’s also worth noting that equals ... between == and equals in Java is that "==" is used to compare primitives while the equals() method is recommended to check the equality of objects....
🌐
Coderanch
coderanch.com › t › 409507 › java › Difference-equals
Difference between == and .equals (Beginning Java forum at Coderanch)
And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org ... marc's comments made me wonder whether my examples were correct or not so I verified that they are correct: I acknowledge marc's point that the programmer has to define the equals() method because it defaults to be the same as the operator, ==. In my example, the class Integer is defined in the Java API and the equals() method is overridden there.
🌐
Wyzant
wyzant.com › resources › ask an expert
What is the difference between == and equals() in Java? | Wyzant Ask An Expert
March 14, 2019 - I wanted to clarify if I understand this correctly: - `==` -> is a reference comparison, i.e. both objects point to the same memory location - `.equals()` -> evaluates to the comparison of values in the objects Am I correct in my understanding ? ... John C. answered • 03/14/19 ... Experienced Java tutor ready to help you succeed!
🌐
Medium
medium.com › @mugheesqasim › difference-between-and-equals-in-java-bad6343d523e
Difference between == and equals() in Java | by Mughees Qasim | Medium
August 4, 2023 - The simplest boring difference is that == is an operator that is used to compare values of two variables and returns true if the values stored in the variables are equal and equals() is a method used to compare the actual values of two objects.
🌐
Scaler
scaler.com › home › topics › difference between comparing string using == and .equals() method in java
Difference between Comparing String Using == and .equals() Method in Java - Scaler Topics
June 2, 2024 - The == operator matches memory references and the characters. The equals() method is a function while == is an operator. The == operator is generally used for memory references comparison.