0

As I know

== is used to compare references. .equals() is used to compare content.

There is a string pool is available in java. For example I created a string as follows

String s ="ABC"; then address for this is suppose 12345

When I use same string i.e "ABC" same string will share in both places. That means addresses for both strings are same.

== is for compare references .In this case address for both strings are same.Then why I need to go for equals() method.Please correct me if I am wrong.

public class StringComparison {

public static void main(String[] args)
{
    String s1 ="test";
    String s2 = "test";

    if(s1 == s2)
        System.out.println("BOTH ARE  == ");
    else
        System.out.println("BOTH ARE != ");


    String s3 = new String("test");
    String s4 = new String("test");

    if(s3 == s4)
        System.out.println("BOTH ARE  == ");
    else
        System.out.println("BOTH ARE != ");

  }
}

OUTPUT:
BOTH ARE  == 
BOTH ARE != 

But the reference link says new String("test") == new String("test") ==> false

László Papp
  • 51,870
  • 39
  • 111
  • 135

0 Answers0