public String makinStrings() {
String m = "Fred47";
String s = "Fred";
s = s + "47";
s = s.substring(0);
return s.toString();
}
How many objects are created by the code?
I made a simple test:
public static void main(String[] args) {
String m = "a";
m += "bc";
String s1 = "mabc".substring(1);
String s2 = "abc";
System.out.println(m == "abc");
System.out.println(m == s1);
System.out.println(m == s2);
System.out.println(s1 == s2);
}
Shouldn't the result be "true, true, true, true", if m, s1, s2 point to the same object ("abc")? Instead the result is "false, false, false, false"!