0

public class EqualsTest {

public static void main(String[] args) {

    String s1 = "abc";
    String s2 = s1;
    String s5 = "abc";
    String s3 = new String("abc");
    String s4 = new String("abc");
    System.out.println("== comparison : " + (s1 == s5));
    System.out.println("== comparison : " + (s1 == s2));
    System.out.println("Using equals method : " + s1.equals(s2));
    System.out.println("== comparison : " + s3 == s4);
    System.out.println("Using equals method : " + s3.equals(s4));
}

}

Output:

== comparison : true
== comparison : true
Using equals method : true
false
Using equals method : true

Can anyone explain how the output of s3==s4 became false in the above, also how is this equals tag operation, i mean its working?

Ankit
  • 6,554
  • 6
  • 49
  • 71
  • 1
    Congratulations for posing the number 1 most frequently asked question on StackOverflow. – Marko Topolnik Jul 28 '13 at 08:06
  • When you post a very common question, it demonstrates that you made no effort at all to research the topic first. Doing some research yourself is important as a) you might find you are not the first person in the world to think of this problem b) you have a better chance of understanding the answer. The only thing more frustrating than seeing the same questions again and again, is people asking questions they have no hope of understanding the answer for. – Peter Lawrey Jul 28 '13 at 10:36

3 Answers3

0

The "==" operator compares memory location (do both these variable names point to the same instance) whereas ".equals()" compares the value (even if distinct copies, do they share the same content?)

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

If I'm not mistaken, == operator compares objects when you have s3 and s4 defined like that. Since objects aren't the same (don't have the same address or memory location; often called reference) - the operator gives false. The == operator works on the other examples because you are comparing primitive strings. This is a reason (at least in my experience) that == is not a good choice and you should always use .equals in order to be sure that you are comparing strings and not objects themselves.

daxur
  • 359
  • 1
  • 11
0

Obviously s3 and s4 equal with value but not object wise. So s3==s4 is false since it is compare memory locations.

You may learn the difference between == and equals() in java.

The “==” operator

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.

The equals() method

The equals method is defined in the Object class, from which every class is either a direct or indirect descendant. By default, the equals() method actually behaves the same as the “==” operator – meaning it checks to see if both objects reference the same place in memory. But, the equals method is actually meant to compare the contents of 2 objects, and not their location in memory.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115