0

Consider this below example,

String str1="Amazon ";
String str2="Netflix";
String str3=str1+str2;
String str4="Amazon Netflix";
System.out.println(str3.equals(str4));
if (str3==str4)
{
    System.out.println("They Match");
}
else
{
    System.out.println("They dont Match");
}

We know String object are immutable, but why java creates another string object str4 since str3 is reference to "Amazon Netflix" I know we can use str3.equals(str4)); but just curious how does it happens with str4. Can someone explain this.

J_Coder
  • 707
  • 5
  • 14
  • 32
  • That's not exactly how it works... the reference isn't to the value. It's to an address in the memory, which happens to contain that value. `.equals()` checks if two references point to the same address. (I think. not 100% sure about this, which is why it's not posted as an answer) – ItamarG3 May 06 '17 at 13:21
  • 2
    `.equals()` doesn't check addresses, it checks the contents of each string. – byxor May 06 '17 at 13:22
  • @byxor and their types ^^. – Ousmane D. May 06 '17 at 13:23
  • 1
    I cannot understand your question – Yahya May 06 '17 at 13:23
  • 1
    You can get what you want by making the variables `final`. See http://stackoverflow.com/a/15427670/14955 – Thilo May 06 '17 at 13:24
  • `"I know we can use str3.equals(str4)); but just curious how does it happens with str4"`. Just curious how _what_ happens with str4? – byxor May 06 '17 at 13:24
  • 1
    The concatenation happens at runtime. Java can't know, before the concatenation has been made, if it already has a String equal to the result of the concatenation in its pool. Since the concatenation has been made anyway, there is no reason to lose time again to try finding an equal string and return that one instead of the one it just created. – JB Nizet May 06 '17 at 13:25
  • @JBNizet: You can make the concatenation happen at compile-time by making the variables final. Then both Strings end up being the same interned instance. – Thilo May 06 '17 at 13:27
  • this [Strings of Same Value in Java?](http://stackoverflow.com/questions/6558285/strings-of-same-value-in-java) answers your question. – Ousmane D. May 06 '17 at 13:27
  • 1
    @Thilo agreed. In fact I even expected the compiler to detect that they're effectively final, but that doesn't happen, apparently. – JB Nizet May 06 '17 at 13:28
  • @JBNizet: I think the JLS prevents the compiler from doing that. It has a very strict definition of what a constant expression is. And some code out there probably depends on that behaviour ... – Thilo May 06 '17 at 13:30
  • 1
    The definition of "constant variable" is a `final` variable assigned by a _constant expression_. An expression comprising only `String` literals, primitive literals, and constant variables, joined by operators (not methods), is a constant expression. Constant variables and expressions are stored in the bytecode as literals. Non-`final` variables such as those here cannot be constants. So the concatenation produces a new instance, as documented in the JLS, which I commend to your attention. – Lew Bloch May 06 '17 at 18:23

0 Answers0