java equal method

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.

4
1
Awgiedawgie 440220 points

                                    if(bob.equals("Sam")){ // Checks if strings are equal.
  // code
}

4 (1 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
what is the java equals method Difference between the equals() and == in java. is equal function in java what is equals() method in java used for ? difference == and equals in java Equal and Equals java why java equals() better than == equal on java how java equal method works java difference between != and .equals What are the differences between == and equals in java .equal to in java difference between equals and equal to in java java equalsignorecae vs equals == or equals in java == vs .equals iun java java using or and equals together java == equals == and .equals java .equals in java vs == equals versus == java differnce betwee == and equals java equals method and == operator in java equals method in javaa string java equals vs == difference between equals() method and equality operator (==) in Java? .equals and == example in java is equals method in java java equals vs == node java compare vs equals difference between == and equals java what's the difference between == and equals in java equals() vs == in java is equals method java != VS equals java equals methode in java java equals and == can equals vs equals java !equal java equals to method in java == vs equals in java string equals vs object equals java what does .equal do in java !== and equals in java equal in java equal method in java 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 equal() java java contain vs equals what does the equal method in java do .equals java vs == why do we use equals method in java equal to function in java a.equal() in java equal and equal equal method java compareto vs equals java .equal() in java equals java method how to negate equal method in java java object equals vs == difference between .equals and == in java equal vs == java java equal to equal vs equalsto java equal method java Java equals vs == why different equal vs == in java == and .equals differences in java how equal method works in java and equals java equals method in java java contains vs equals java equals vs compareto java should I use == or .equals java when is .equals used what is equal() in java java equals vs == vs === equal in java difference between .equals and == java == vs equals jva 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 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 == 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 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