1
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

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • 2
    This might be an interesting read for you http://stackoverflow.com/questions/3029244/are-strings-created-with-concatenation-stored-in-the-string-pool – Em Ae Feb 08 '16 at 17:41
  • 2
    Your assumption would be true if `s` were declared `final`. – Andy Turner Feb 08 '16 at 17:42

5 Answers5

3

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

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

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.

Em Ae
  • 8,167
  • 27
  • 95
  • 162
  • The difference is that you have a constant expression, whereas OP doesn't. And that's why you get `true`. – Tom Feb 08 '16 at 17:53
0
String grima = "grima";
String gri = "grima";
if (grima == gri)
    System.out.println("grima == gri"); // The message is displayed

Both variables refer to same "grima" in string constant pool.

String pndey = "pandey";
String pan = "pande";
pan = pan + "y"; // The value for pan will be resolved during runtime.
if (pndey == pan)
    System.out.println("pndey == pan"); // The 2 references are different

Now in the second case if I change

pan = pan + "y"; to pan="pande" +"y", then "pndey == pan" message will be displayed, because the compiler resolves the literal during compilation time.

Now as per Java docs on string:

  • Literal strings within the same class (§8 (Classes)) in the same package (§7 (Packages)) represent references to the same String object (§4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run time are newly created and therefore distinct.
  • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

You read more here: https://codingninjaonline.com/2017/09/18/stringstring-constant-pool-in-java/

Tom
  • 16,842
  • 17
  • 45
  • 54
pndey
  • 71
  • 4
-2

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

Akhil
  • 78
  • 12
  • 1
    that's not what he's asking for - I think – Sebas Feb 08 '16 at 17:45
  • This question is not about the generally correct way of comparing Strings, it is about the behaviour of the String constant pool and comparing String constants. – Tom Feb 08 '16 at 17:58
-2

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
    
Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • This question is not about the generally correct way of comparing Strings, it is about the behaviour of the String constant pool and comparing String constants. – Tom Feb 08 '16 at 17:58
  • i have changed the answer – Iamat8 Feb 08 '16 at 18:31
  • *"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."* What exactly will be placed there? – Tom Feb 08 '16 at 18:52
  • when s2 will be created firstly it will refer to s and then concat to "class"..that is why it is not referring a single value – Iamat8 Feb 08 '16 at 18:54
  • I'm not talking about the single values of `s` and `"class"`. You're just a bit unclear, why OP gets `false`. Since he seems to be new, you should be a bit more clear :). – Tom Feb 08 '16 at 19:04