2

I was in an interview and got a question as below:

String s = "abcde5";
String s1 ="abcde"+"5";
System.out.println(s==s1);
System.out.println(s.equals(s1));
String s4 = "abcd5";
String s3 = "abcd"+s4.length();
System.out.println(s3);
System.out.println(s3.equals(s4));
System.out.println(s3==s4);

I thought that the output will always yield false for "==" as using the + operator will create a new string same as doing the s4.length. but the expected answer is different. Can anyone please let me know why s==s1 gives true, and s3==s4 yields false.

expected answer is: true true abcd5 true false

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sumit Sagar
  • 342
  • 1
  • 6
  • 17

1 Answers1

1

s==s1 yields true because "abcde"+"5" is a constant expression, i.e. an expression composed of string literals and concatenations. All Java compilers must treat them as a single literal, i.e. as if the + were not there.

Java language standard carefully makes an exception for compile-time constant string expressions when explaining new object creation on string concatenation:

A new class instance is implicitly created when the string concatenation operator + (§15.18.1) is used in a non-constant (§15.28) expression, resulting in a new object of type String (§4.3.3).

Expression "abcd"+s4.length(), on the other hand, is not a constant expression. That is why a new object is created for it, causing s3==s4 to yield false.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I read this here:http://stackoverflow.com/questions/10578984/what-is-string-interning?noredirect=1&lq=1 that `System.out.print((hello == ("Hel"+lo)) + " ");` where `static String hello = "Hello";` and `String lo = "lo";` this will return false, because concatenating "lo" is not a constant expression? – Sumit Sagar Jan 07 '17 at 08:32
  • @Sumitsagar Yes, `lo` is not a string literal. The example above it, i.e. `hello == ("Hel"+"lo")` produces `true`. – Sergey Kalinichenko Jan 07 '17 at 08:37
  • So anything other than concatenating string literals "==" will always yield false?and can you please tell what is (§4.3.3) symbol? thank you. – Sumit Sagar Jan 07 '17 at 08:42
  • @Sumitsagar No, other constant expressions are OK (e.g. `"23"=="2"+3`, [demo](http://ideone.com/d2QlOR)). § is a reference to [Java Language Specification](http://download.oracle.com/otn-pub/jcp/jls-7-mr3-fullv-oth-JSpec/JLS-JavaSE7-Full.pdf?AuthParam=1483778786_5d50cae8ca7acf382d2d11c294941486). – Sergey Kalinichenko Jan 07 '17 at 08:47
  • I tried a few more.. like `s==s1+5` will yield false. or `s==s1+a` //where int `a=5`. and `s==(sn+"5")` will give false too, because compiler has to resolve their values at compile time? but `s=="abcde" +5` will give true, because in simple terms the compiler doesn't have to evaluate the variable. – Sumit Sagar Jan 07 '17 at 09:06
  • @Sumitsagar Please read section §4.3.3 of the language spec, it should give you clarity on the difference between constant expressions and non-constant expressions. – Sergey Kalinichenko Jan 07 '17 at 09:10