Which of the following are output by this code? (Choose all that apply)
3: String s = "Hello"; 4: String t = new String(s); 5: if ("Hello".equals(s)) System.out.println("one"); 6: if (t == s) System.out.println("two"); 7: if (t.equals(s)) System.out.println("three"); 8: if ("Hello" == s) System.out.println("four"); 9: if ("Hello" == t) System.out.println("five");
A, C, D.
The code compiles fine.
Line 3 points to the String in the string pool.
Line 4 calls the String constructor and is a different object than s.
Lines 5 and 7 check for object equality, which is true, and so print one and three.
Line 6 uses object reference equality, which is not true since we have different objects.
Line 7 compares references but is true since both references point to the object from the string pool.
Line 8 compares one object from the string pool with one that was constructed and returns false.