top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What's the difference between "a == b" and "a.equals(b)" in Java?

+2 votes
2,397 views
What's the difference between "a == b" and "a.equals(b)" in Java?
posted Apr 1, 2016 by Deepak Jangid

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

0 votes

"==" if used for the compare the objects where as the "equals()" is used to compare the content of the object.

== operator is the equality operator
equals() is for content comparison
== operator checks if both object references refer to the same memory location eg: if (a==b) it means it verifies whether both a and b refer to the same location in memory.
equals() if for content comparison.
eg:String a ="chandra"; String b="chandra"; String c ="radha";
if ( a.equals(b)) returns true bcoz both contains the same content.
if (a.equals(c)) returns false bcoz a and c contents are different.

answer Apr 1, 2016 by Karthick.c
0 votes

"==” compares reference – returns true if and only if both references point to the SAME object
while
"Equals" method compares object by VALUE and it will return true if the references refers object which are equivalent .
Also... Equals method compare not only value but it compares TYPE also in Value Types.

answer Apr 4, 2016 by Ashutosh Kumar Anand
0 votes

"a == b" ##:

It compares references not values. Return type is boolean. == is used in rare situations where you know you’re dealing with interned strings.

**

"a.equals(b)"

It compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string.For example,if str1 and str2 are two string variables then if
**

Example for String

// Java program to show how to compare Strings

public class Test
{
    public static void main(String[] args)
    {
        String s1 = "Ram";
        String s2 = "Ram";
        String s3 = new String(" Ram");
        String s4 = new String(" Ram");
        String s5 = "Shyam";
        String nulls1 = null;
        String nulls2 = null;

        System.out.println(" Comparing strings with equals:");
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s5));

        System.out.println(" Comparing strings with ==:");
        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s3==s4);

        System.out.println(" Comparing strings with compareto:");
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.compareTo(s5));
    }
}

Output:

Comparing strings with equals:
true
true
false
Comparing strings with ==:
true
false
false
Comparing strings with compareto:
0
-1

answer Jan 6, 2017 by Ajay Kumar
Similar Questions
+2 votes

Given 2 strings, a and b, return a string of the form shorterString+longerString+shorterString, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). If input is "hi" and "hello", then output will be "hihellohi".

...