2

In Java,

String s1 =  new String("abc");
String s2 = new String("ab") + new String("cd");

Will string "abc" and "abcd" be created in string constant pool?

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
bob
  • 37
  • 1

1 Answers1

1

Neither s1 nor s2 will point to strings in the constant pool because you created them by a combination of explicit constructor calls and concatenation. Only the string literals "abc", "ab", and "cd" will be in the constant pool.

You would need to explicitly intern() the new instances. In that case, s1 == "abc" would become true.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436