4

When I concat 2 strings with (+) operator using double quotes and compare with other string literal with same value it result as true .. but when I concat 2 string variables and compare gives false ? why this happens ?

As per my knowledge when we concat strings with (+) operator JVM returns new StringBuilder(string...).toString() which creates a new String instance in heap memory and one reference in String pool . if that is true how is it returning true in one scenario and false in other?

1st scenario :

    String string1 = "wel";
    String string2 = "come";
    string1 = string1 + string2; //welcome

    String string3 = "welcome";
    System.out.println(string3 == string1); // this returns false but have same hashcode

2nd scenario :

    String string4 = "wel" + "come";
    String string5 = "wel" + "come";
    System.out.println(string4 == string5); // this returns true

Can someone help me on this ?

  • 2
    If you concat non-`final` `String` variables, yes, you will have a `StringBuilder`. Not if you concat `String` literals, ie. constants. – Sotirios Delimanolis Oct 21 '13 at 14:24
  • 1
    always compare strings with `equals()` method as `string3.equals(string1);` –  Oct 21 '13 at 14:27
  • 1
    possible duplicate of [Comparing two identical strings with == returns false](http://stackoverflow.com/questions/7375427/comparing-two-identical-strings-with-returns-false) – Dariusz Oct 21 '13 at 14:28
  • 4
    "As per my knowledge when we concat strings with (+) operator JVM returns new StringBuilder(string...).toString() which creates a new String instance in heap memory" isn't true when you're concatenating String literals in the source code, e.g., `"wel" + "come"`. That kind of concatenation is done at compile time, and it's the same as if you had written `"welcome"`. This is pointed out in [Suresh Atta's answer](http://stackoverflow.com/a/19497248/1281433). – Joshua Taylor Oct 21 '13 at 14:50
  • @user2511414 : I am using == here to check whether both values refer to same instance in the String pool or not, that is the only way to verify String pool references. The objective is to understand how string pool works. – manoj bhide Oct 21 '13 at 15:25
  • A better way to interpret this isn't that `Strings` **literals** get interned in the string pool, it's String **constants**. This includes constants that are created in the process of [*constant folding*](http://ideone.com/ZxmLzN) by the compiler: http://ideone.com/ZxmLzN – millimoose Oct 22 '13 at 01:00

2 Answers2

4

Please follow comments.

 string1 = string1 + string2; //StringBuilder(string...).toString() which creates a new instance in heap..

 String string3 = "welcome"; // It is not in heap. 

So string3 == string1 false

  String string4 = "wel" + "come"; //Evaluated at compile time

  String string5 = "wel" + "come"; //Evaluated at compile time and reffering to previous literal

So string4 == string5 is true

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

As per my knowledge when we concat strings with (+) operator JVM returns new StringBuilder(string...).toString() which creates a new String instance in heap memory

Correct, unless both operands are literals, in which case a single string constant is created at compile time and pooled.

and one reference in String pool.

False. The only things in the String pool are String constants and String.intern() return values.

if that is true

It isn't.

how is it returning true in one scenario and false in other?

Because your premiss is incorrect.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • "a single literal is created at compile time" - this is inaccurate on a nitpicky level. A *literal* is the actual syntactical feature - expressions like `123` or `"abc"` in the source code. At compile-time, an entry in the *constant pool* is created - the compiler doesn't generate source code so it doesn't create literals. (I also wouldn't say that a *constant* is created, since it's a quality of variables, not of values, values would be immutable - which all String values are regardless of whether they're literals or not.) – millimoose Oct 22 '13 at 01:06
  • 1
    @millimoose I agree about 'string literal', and I had already fixed that, several minutes before your comment. I disagree about 'constant'. That's why it's called a 'constant pool'. For constants. There are many constants that aren't variables. String literals, for a start :-| – user207421 Oct 22 '13 at 01:27
  • The part in the parents I'll admit is in the realm of splitting hairs over semantics, I just added that to complete my thought, not to point out an error. – millimoose Oct 22 '13 at 10:59