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

🌐
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.
🌐
Quora
quora.com › Whats-the-difference-between-a-and-in-Java
What's the difference between a = and == in Java? - Quora
Answer (1 of 33): “=” is for assigning a value to a variable. For example, [code]int a=5; [/code]here you are assigning value 5 to a variable named ‘a’. You can also assign a variable to a variable. (Actually you are assigning value of one variable to the other variable).
🌐
LabEx
labex.io › questions › what-is-the-difference-between-and-in-java-178545
What is the difference between = and == in Java? | LabEx
The == operator can be used to compare values of primitive types, but it compares the references (memory addresses) of reference types. Here's a Mermaid diagram to illustrate the key differences:
🌐
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.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › core java › difference between == and equals() in java
Difference Between == and equals() in Java | Baeldung
November 27, 2025 - Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system: >> Flexible Pub/Sub Messaging With Spring Boot and Dapr · Understanding how Java evaluates equality is important when working with both primitive values and objects. Different mechanisms apply depending on whether the comparison checks references or the internal state of an object, and these differences influence how equality behaves in common scenarios.
🌐
Quora
quora.com › What-is-the-difference-between-in-Java
What is the difference between = , == , === in Java? - Quora
= is used when you want to assign a string/ integer/float to a variable. ex: int i=5; // here 5 is assigned to variable i. == is used when you want to compare two variable. ex: int s1=5 , s2= 5; if(s1==s2){ System.out.println(...
Find elsewhere
🌐
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.
🌐
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.
🌐
Unacademy
unacademy.com › gate cse it › difference between operator and equals method in java
Difference Between Operator and Equals Method in Java
October 2, 2022 - Considering java.lang. The string class overrides the equals() and the hashcode methods, and in the override methods, it is determined whether two strings contain the same data or character and, if so, whether they are equal. If not, they are not. It’s an opportunity to compare the objects now that we understand the equals function, how it checks objects, and what the equality operator (==) means. The following are some important distinctions between Java’s equals() function and == operator:
🌐
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?

🌐
Java67
java67.com › 2012 › 11 › difference-between-operator-and-equals-method-in.html
Difference between == and equals() method in Java? String Example | Java67
For example java.lang.String class overrides the equals() and hashcode method and in the overridden method, it will check that two string contains same value or character if yes then they are equals otherwise not equal. Now what you know what is equals method, how it works and What is equality operator (==), and How it compares objects, it's time to compare them. Here is some worth noting difference between equals() method and == operator in Java:
🌐
BYJUS
byjus.com › gate › difference-between-operator-and-equals-method-in-java
Difference between == and .equals() method in Java
March 29, 2023 - == is an operator that compares the memory or reference location of an object in the heap. .equal() is a method that compares the actual content of the object. Keep learning and stay tuned to BYJU’S to get the latest updates on GATE Exam along ...
🌐
TutorialsPoint
tutorialspoint.com › equals-vs-in-java
equals() vs == in Java
The.equals() function compares the contents of two objects. The == operator compares the references of two objects. When you create an object with the new operator, it gets assigned a specified memory location in the heap. Tak, for example, two objects are having the same data.
🌐
Stackademic
blog.stackademic.com › java-equals-vs-understanding-the-key-differences-d0c8899992a2
Java equals() vs == – Understanding the Key Differences | by M Business Solutions | Stackademic
December 9, 2024 - When you’re learning Java, one of the most common areas of confusion is the difference between the equals() method and the == operator. At first glance, both seem to compare two objects, but they function very differently under the hood. Understanding the distinction is crucial because using one in ...
🌐
Sololearn
sololearn.com › en › Discuss › 163136 › what-is-the-difference-between-and-
What is the difference between " = " and " == " ?? | Sololearn: Learn to code for FREE!
= is assignment, like if you wanna assign x as 2, you do int x = 2. == is comparison, if you wanna compare x and 2, you do if (x == 2), either true or false. ... "=" is an assignment operator which is used to assign value to the variable ex: ...