Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two strings with "==": when will it work?

Say you have three strings,

String s1 = "string one";
String s2 = new String("string one");
String s3 = "string one";

I know it is true that s1 == s2 is false, but I read somewhere that s1 == s3 is true. Is this correct? Why or why not?

like image 297
Alex Lockwood Avatar asked Sep 20 '11 02:09

Alex Lockwood


People also ask

Can you use == when comparing strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What happens when you compare two strings with the == operator?

String Comparison With Objects Class The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.

What happens when you use == with strings?

When we use the == operator for s1 and s2 comparison, the result is true as both have the same addresses in the string constant pool. Using equals, the result is true because it's only comparing the values given in s1 and s2.

When comparing strings Why is == not a good idea?

1) You can compare two String variable using == operator but you should never do this because it will return true if you compare String literals but return false if you compare String object to a literal or two String object, even if they have same characters.


1 Answers

String literals are interned automatically. Hence s1 == s3 is true. Strings can either be created in the string constant pool or they can be created in the heap space. If you intern a string created in the heap, the string will be in the string constant pool.

When you create a string literal (String s1 = "string one"), the string is created in the string constant pool. Additionally, the string constant pool doesn't store duplicates. So when you say,

String s1 = "string one";
String s3 = "string one";

Both s1 and s3 will be pointing to the same instance of the string in the string constant pool. So s1.equals(s3) will be true. And s1 == s3 also is true; since both the pointers are the same.

However, when you instantiate a string using the "new" constructor

String s2 = new String("string one");

then s2 is created in the heap space. The heap space is a different area of memory than the string constant pool

So while s1.equals(s2) is true, s1 == s2 is false; since they will be pointing to different areas of memory.

However, you can convert a string created using the "new" constructor to make it move to the string constant pool by invoking the intern() function. So s2.intern() will return a string in the string constant pool; although s2 was originally created in the heap.

like image 81
Basanth Roy Avatar answered Nov 11 '22 17:11

Basanth Roy