0

I have the following simple piece of java code where I am trying to understand how string concatenation in java works using '+' operator.

public class Problem {

public static void main(String... args){

    String str1 = "abc";
    String str2 = "ab";
    String str3 = "c";
    String str4 = "ab" + "c";//This will use of  StringBuilder class for concatenation and return new String object
    String str5 = str2 + str3;//This will use of  StringBuilder class for concatenation and return new String object

    System.out.println(str1 == str4); // This  returns true
    System.out.println(str1 == str5); // This  returns false

}
}

str4 is the resultant of 2 string literals (ab and c) and str5 is of references to the 2 string literals (str2 and str3). In both the cases, java will be calling StringBuilder class to perform the concatenation.

And I believe it should result in creating 2 different StringBuilder objects in java heap space.

If my understanding is correct, why str1 == str4 returns true ? Can some one please help in getting this clear to me ?

Regards, Maneesh Sharma

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maneesh
  • 119
  • 10
  • Possible duplicate of [What is the difference between “text” and new String(“text”)?](https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext). – Tim Biegeleisen Nov 11 '18 at 03:37

1 Answers1

0

It depends on how the compiler and jvm will optimize this. Only if both variables are optimized to point to the same adress, the evaluation will be true. In general, using == to compare strings is not reliable. Use the 'equals' method instead.

Pasukaru
  • 1,050
  • 1
  • 10
  • 22
  • So the output of the above program may differ for different JVMs, is that correct ? – Maneesh Nov 11 '18 at 11:32
  • I'm not 100% sure about the specification for this. But in practice, yes. The address of new variables should be treated as nondeterministic. – Pasukaru Nov 11 '18 at 12:30
  • 1
    One more note: The jvm *may* optimize immutable objects (especially strings) by pointing their variables to the same address. But you don't have control over that. Also, I imagine searching the occupied address space for an equal object is expensive. I don't know in which cases it could do these optimizations. – Pasukaru Nov 11 '18 at 12:32
  • Thank you Pascal for the additional info ! I am still wondering if I am given the above problem as an objective question, what my guess should be ? If I go by the explanations so far I came across how string concatenation works, my answer would be `false` for `str1 == str4` but is not the case :) ! – Maneesh Nov 11 '18 at 13:25
  • While browsing I found an article in stackoverflow where one of the comments mentioned the following.. **if you simply write "ab" + "c", Java compiler will perform concatenation at compile time and the generated code will be exactly the same. This only works if all strings are known at compile time. If any of the strings is a variable and cann't be determined by compiler, it will have a different resultant hash code** – Maneesh Nov 12 '18 at 08:36