Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardJavaString.intern and poolSingle-choice MCQ

Two distinct String objects with equal contents are interned. After s1 = a.intern() and s2 = b.intern(), what does the JVM guarantee about identity, and why?

String a = new StringBuilder("jav").append("a").toString(); String b = new StringBuilder("ja").append("va").toString(); String s1 = a.intern(); String s2 = b.intern(); String lit = "java"; System.out.println((s1 == s2) + " " + (s1 == lit));