1

Yesterday this Question asked by Interviewer what is the output for that line.

public class StringTest {

  public static void main(String[] args) {
      String s1 = "abc";
      String s2 = "abc";
      String s3 = s1+s2;
      String s4 = s1+s2;

      //check s1 == s2
      System.out.println(s1==s2);

      //check s3 == s4
      System.out.println(s3==s4);
  }
}

when i looked at this question then thinking how the easy question asked by interviewer. I told him output s1==s2 and s3==s4 will return true and trueand i was very confident. Suddenly he said no thats the wrong output then i thinking he might be joking or trying to loosing my confidence, but till the end he said its wrong. When i checked the output was true and false . I also thinking how it is possible please solve my confusion by giving a appropriate answer. thanks in advance.

amit
  • 175,853
  • 27
  • 231
  • 333
Roushan
  • 4,074
  • 3
  • 21
  • 38
  • This is the very first of it's kind question on String's. Thankyou. – Suresh Atta Aug 25 '15 at 17:02
  • @sᴜʀᴇsʜᴀᴛᴛᴀ forgot the /s – amit Aug 25 '15 at 17:02
  • @amit i can't find my answer http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Roushan Aug 25 '15 at 17:08
  • @amit why r u marked this duplicate .... please do check for this type of question explanation is not given. – Roushan Aug 25 '15 at 17:14
  • Because I find `s1+s2` and `new String(s1)` to be basically the same concept, and the later is answered with details. – amit Aug 25 '15 at 17:16
  • There were other questions with the same topic (concatenation of string literals and `==` returns false), but I couldn't find any of them. May they have bad title so OP couldn't find them either :D. Edit: mhh, maybe this is a good one: [Comparing strings with == which are declared final in Java](http://stackoverflow.com/q/19418427) – Tom Aug 25 '15 at 17:29

2 Answers2

6

s1+s2 is not known at compile time so it is calculated at runtime and creates a new object, which is different each time it is run. If you change s1 and s2 to be final, the compiler will inline the constants and you will get true for s3==s4.

  final String s1 = "abc";
  final String s2 = "abc";
  String s3 = s1+s2; // compiler replaces with "abcabc"
  String s4 = s1+s2; // compiler replaces with "abcabc"

  //check s1 == s2
  System.out.println(s1==s2);

  //check s3 == s4
  System.out.println(s3==s4); // is now true.

OR

  String s1 = "abc";
  String s2 = "abc";
  String s3 = (s1+s2).intern(); // use the string literal pool
  String s4 = (s1+s2).intern();

  //check s1 == s2
  System.out.println(s1==s2);

  //check s3 == s4
  System.out.println(s3==s4); // is now true.

BUT

  String s1 = "abc";
  String s2 = "abc";
  // String s3 = s1+s2;
  String s3 = new StringBuilder().append(s1).append(s2).toString();
  // String s4 = s1+s2;
  String s4 = new StringBuilder().append(s1).append(s2).toString();

  //check s3 == s4
  System.out.println(s3==s4); // different objects.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-1

Because strings are immutable and are ubiquitous in programming, the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string.
Lets say:

String s1 = "xyz";
String s2 = "xyz";

In the preceding statements, s1 and s2 refer to the same interned string—"xyz"—so s1 == s2 is true. This is not the case with s3 and s4. You can read more about interned Strings on Google.

Tom
  • 16,842
  • 17
  • 45
  • 54
Wololo
  • 841
  • 8
  • 20