Difference between == operator and equals method in java

In general both equals() and == operator in Java are used to compare 
objects to check equality but here are some of the differences between the two:

1) .equals() and == is that one is a method and other is operator.
2) We can use == operator for reference comparison (address comparison) 
and .equals() method for content comparison. 
 -> == checks if both objects point to the same memory location 
 -> .equals() evaluates to the comparison of values in the objects.
3) If a class does not override the equals method, then by default it 
uses equals(Object o) method of the closest parent class 
that has overridden this method.

// Java program to understand  
// the concept of == operator 
public class Test { 
    public static void main(String[] args) 
    { 
        String s1 = new String("HELLO"); 
        String s2 = new String("HELLO"); 
        System.out.println(s1 == s2); 
        System.out.println(s1.equals(s2)); 
    } 
} 
Output:
false
true
  
Explanation: Here we are creating two (String) objects namely s1 and s2.
Both s1 and s2 refers to different objects.
 -> When we use == operator for s1 and s2 comparison then the result is false 
 as both have different addresses in memory.
 -> Using equals, the result is true because its only comparing the 
 values given in s1 and s2.

3.86
7
Awgiedawgie 440220 points

                                    // == operator
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2); // output : false


// equals method
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // output : true

3.86 (7 Votes)
0
4.5
8
Phoenix Logan 186120 points

                                    // Java program to understand 
// the concept of == operator and .equals() method
public class Test {
    public static void main(String[] args)
    {
        String s1 = new String("HELLO");
        String s2 = new String("HELLO");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

Output:
false
true

Explanation:
We can use == operators for reference comparison (address comparison) and 
.equals() method for content comparison. In simple words, == checks if both 
objects point to the same memory location whereas .equals() evaluates to the 
comparison of values in the objects.

4.5 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Difference between the equals() and == in java. difference == and equals in java Equal and Equals java why java equals() better than == difference between equals method and == operator == operator and equals method in java java difference between != and .equals What are the differences between == and equals in java difference between equals and equal to in java java equalsignorecae vs equals == or equals in java == vs .equals iun java java using equals and OR (||) operator together java using equals and OR operator together java using or and equals together java == equals what is the differnecde between equals and canEqual in Java == and .equals java .equals in java vs == equals versus == java 5.Can you tell the difference between equals() method and equality operator (==) in Java? what different between == and equal in java differnce betwee == and equals java equals method and == operator in java string java equals vs == difference between equals() method and equality operator (==) in Java? .equals and == example in java java equals vs == node diff equals and == in java Differentiate between == operator and equals() method in string. java java compare vs equals difference between == and equals java what's the difference between == and equals in java equals() vs == in java != VS equals java java equals and == java what is the diference between == and equals can equals vs equals java == vs equals in java string equals vs object equals java !== and equals in java java difference between compareto and equals why use .equals instead of == java difference between equals() and == in java .equals and == in java diferença equals == java string == vs equals java compare() vs equals() java compare vs equals java == and equals in java difference equal and == difference in java java contain vs equals .equals java vs == Can you tell the difference between equals() method and equality operator (==) in Java? compareto vs equals java difference between equals and equal in java java object equals vs == difference between .equals and == in java equal vs == java difference betweeen == and .equals() in java equal vs equalsto java Java equals vs == why different equal vs == in java == and .equals differences in java and equals java java contains vs equals java equals vs compareto java should I use == or .equals java when is .equals used java equals vs == vs === difference between .equals and == java == vs equals jva difference equals and == java string equals vs == java == and equals java java difference between equals and == java === vs equals() equals vs == java object equals vs == java string java equal method difference between == equals java difference equals and == difference between == and equals() in java !equal() differences between equals and == in java java compareto vs equals .to.equal() .equals vs == on strings == between strings in java when to use .equals in java == vs .equals in java difference between == and === in java string == java equals method .equals method in java What is the difference between the equals method and the == operator in Java? .equals operator in java equals method java -> == java == operator java .equal vs == java difference between equals method and == operator in java == vs = java java when to use .equals vs == equal and equals in java == vs equals() in java difference equals and default equals for string in java when use equals in java difference between == and equals( isEquals() equals on java java if equal different betweeen!= or !equals java difference between equals() and == in java javatpoint equal() vs == string == vs equals java equals or == equals and equal to in java is == the same as .equals in java java == vs equalsq difference between == and .equals in java java is equal equals method in math class and string class java difference between == and equals what is == java is == and equals the same java what is == and equals in java java string == string java equality java .equals or what is the difference between == and equals in java == java java when to use == operator == equals java is equal java difference between .equals and == .equals("$") java object.equals vs == in java which is equals to in a string .equals vs == in java == meaning java diff between equals and == What is the difference between == and equals () in Java? java equals operator What is the difference between equals() and == in Java? equals in java equals jaba java string comparison equals vs == equals() vs == Difference between == operator and equals method in java Difference between equals() and == java object.equals vs == java .equal java .euqla == vs isequal java java .equals and == == comparison java difference .equals == When the string objects are compared with euqal to operator then the result is true if both the string contains same values EQUAL AND == IN JAVA == on string java == vs .equals check equality with any type in java == equals 7. What is the difference between the two operators, equals() and ==? difference between == and .equals() equal() java equals where do we use == in java diff between equals and == in java .eqauals java equals and === in java What is the difference between equality of objects and equality of references that refer to them? Support your answer with the help of Strings difference between equality of objects and equality of reference that refer to them with string example string == string java what is difference between == and equals in java string == java diference == equals java equals method in java and == if .equals java equals vs == equals() and == difference b/w equals and == in java java string equals vs == difference between equal() and == .equals() java different equals in java equality java when can you use equals java objects.equals vs == equals() vs == java when to use .equals and == in java java == and = .equals vs == java object .equals() == and equals string == in java When are the equals method and the == operator the same java difference between == and equals == vs equals .equals vs == .equals() vs == java string.equals vs == java == ? diff between == and equals in java or in .equals java == string java isequal == vs equal in java .equalsTo java equals java The == operator can be used to compare two String objects. The result is always true if the two strings are identical. True False what is difference between equals and == in java difference between == and equals in java difference between == and equal in java == and isEqual java a ==b java == and .equals in java equals() and == in Java what is the difference between .equals and == in java what is the difference between using == and .equals on an object == in java dot equals java equals() java java == == operater and equels method in java java = and == and ? java .equals vs == java == vs .equalto equal to and == java equals vs == in java difference between equals and == in java equals comparator and == in java equals and == in java java string == vs equals equals == java .equals vs == java What is the difference between equals() and == in Java java difference between .equals and == strings string == , equals isEquals java equals vs == java .equals java difference between == and equals in java string java .equals how == works in java == vs .equals java string == vs .equals java == java equals vs == in java string the difference between == and equals in java equal java java equal == and equals in java java equals vs == == vs equals java java == vs equals string java == vs equals
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source