Consider the following Java code snippet:
public class Test {
public static void main(String[] args) {
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b);
System.out.println(a == c);
}
}What will be the output when this code runs?
public class Test { public static void main(String[] args) { String a = "hello"; String b = "hello"; String c = new String("hello"); System.out.println(a == b); System.out.println(a == c); } }
Remember that == compares references, not content, and string literals are interned.
Strings declared as literals like a and b point to the same object in the string pool, so a == b is true. But c is a new String object, so a == c is false.
Why did Java designers make String objects immutable?
Think about safety and sharing in multi-threaded programs.
Immutability allows strings to be shared safely across threads and reused from the string pool without risk of accidental changes.
Examine this Java code:
public class Test {
public static void main(String[] args) {
String s = "abc";
StringBuilder sb = new StringBuilder(s);
sb.append("def");
System.out.println(s);
System.out.println(sb.toString());
}
}What will be printed?
public class Test { public static void main(String[] args) { String s = "abc"; StringBuilder sb = new StringBuilder(s); sb.append("def"); System.out.println(s); System.out.println(sb.toString()); } }
Remember that String is immutable but StringBuilder can change.
The original String s remains unchanged as "abc". The StringBuilder sb appends "def" and prints "abcdef".
Look at this Java code snippet:
public class Test {
public static void main(String[] args) {
String s = null;
if (s.equals("test")) {
System.out.println("Match");
} else {
System.out.println("No match");
}
}
}What happens when you run this code?
public class Test { public static void main(String[] args) { String s = null; if (s.equals("test")) { System.out.println("Match"); } else { System.out.println("No match"); } } }
Think about calling a method on a null reference.
Calling equals on a null String causes a NullPointerException at runtime.
Consider this Java code:
public class Test {
public static void main(String[] args) {
String[] arr = new String[] {"cat", new String("cat"), "dog", new String("dog")};
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].intern();
}
java.util.Set set = new java.util.HashSet<>();
for (String s : arr) {
set.add(s);
}
System.out.println(set.size());
}
} What number will be printed?
public class Test { public static void main(String[] args) { String[] arr = new String[] {"cat", new String("cat"), "dog", new String("dog")}; for (int i = 0; i < arr.length; i++) { arr[i] = arr[i].intern(); } java.util.Set<String> set = new java.util.HashSet<>(); for (String s : arr) { set.add(s); } System.out.println(set.size()); } }
Remember what intern() does to strings.
Interning returns the canonical string from the pool. Both "cat" and new String("cat") intern to the same object. So only two unique strings remain: "cat" and "dog".
