Home Ask Login Register

Developers Planet

Your answer is one click away!

rakesh venkatraman February 2016

java String comparison in concat

package data;
public class A {

    String s = "maew";
        String s2 = s + "class";
        String s1 = "maewclass";
        System.out.println(s2 == s1);

but both will be in string constant pool and if with same content an object is created one more reference will not get created. s2 and s1 should point to same object in string constant pool.so answer should be true why its giving false


Akhil February 2016

you need to use s2.equals(s1) instead of == please go through link provided for more here

Mohit February 2016

Update :

When we create object its reference is placed in the string constant pool memory.

So in your case when program is run...

JVM finds the variable s2 which refers to s + "class" in which s is also referencing maew and will be placed in the string constant pool memory.Then it finds another variable s1 which is referring maewclass. JVM finds two different string references so both the variables s2 and s1 will not be refer for same string (say..maewclass).

At point when class is loaded if two same values are passed it is will refer to previous object rather creating the new one...

  • String objects with same values will always refer to the same String object.

    String s = "a";
    String s2 = "a";
    System.out.println(s.equals(s2));   //-------return true
    System.out.println(s == s2);        //-------return true 
    s   ------------------>  |     |
                             | "a" |
    s2  ------------------>  |_____|    
                     |        Heap          | 
                     | String Constant Pool |
  • String objects created using new operator will be different from literals

    String s = "a";
    String s2 = new String("a");
    System.out.println(s.equals(s2));      //-------return true
    System.out.println(s == s2);           //-------return false

Michael Gantman February 2016

String constant pool is an internal java feature which you should never rely on. For instance the following code will produce "true"

String s1 = "Hello";
String s2 = "Hello";
boolean result = s1 == s2;

But the following code will produce "false":

String s1 = "Hello";
String s2 = new String("Hello");
boolean result = s1 == s2;

String constant pool behavior may change from one java version to another since it is an internal optimization feature. It shouldn't be relied on. In your case, I suspect because you used String s2 = s + "class"; it did create a new instance.

In any case any String comparison MUST be done with method equals() of class String

Em Ae February 2016

I haven't look at the documentation, just did my own testing and what i believe that the reason you are getting "false" is because your S2 was a result of concatenation of an object (s) and the other string.

If you run this code

//          String s = "maew";
        String s2 = "maew" + "class";
        String s1 = "maewclass";
        System.out.println(s2 == s1);

The it is indeed returning true. So I believe, that java is not keeping result of Object.toString + "string" in the stringpool.

Post Status

Asked in February 2016
Viewed 1,824 times
Voted 13
Answered 4 times


Leave an answer

Quote of the day: live life