This is an optimization any decent compiler should make, yes, but there is no guarantee it will happen that I know of.
Type: Posts; User: jps
This is an optimization any decent compiler should make, yes, but there is no guarantee it will happen that I know of.
The compiler has optimized because s1 and s2 were declared with static strings of equal value, and since strings are immutable there was no reason to make two different String objects with the same...
s1 and s2 would refer to two different objects which happen to hold the same value, but s1==s2 is false, they are two different objects
Some code to play with
public class EditableObject {
private String message;
public EditableObject(String message) {
this.setMessage(message);
}
public String getMessage() {
Strings are immutable, so:
a = "before";
b = a; (now they both point to the same string)
a = "after" (now a points to a different string and b still points to the same old string because b was...