How is it possible for two String objects with identical values not to be equalunder the == operator

The == operator compares two objects to determine if they are the same object inmemory. It is possible for two String objects to have the same value, but locatedindifferent areas of memory.

Showing Answers 1 - 6 of 6 Answers

suraj20p

  • Oct 28th, 2011
 

create 2 string like this

String s1=new String("Hello");
String s2=new String("Hello");
System.out.println(s1==s2);

2 independent strings,therefore 2 different memories,therefore == evaluates to false

Gagan goyal

  • Nov 12th, 2011
 

because == operator only does the shallow comparison .means it compare for the two reference is referring to the same object or not.and when we create the object using new keyword it will always be the new object .

if u do like below

String s1="Hello";
String s2="Hello";
the
if(s1==s2)
returns true.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions