Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
String a = "hi";
String b = new String("hi");
System.out.println((a == b) + " " + a.equals(b));
}
}false true
- Expecting
true true: the literal"hi"sits in the string pool, butnew Stringbuilds a separate object, so==on the two references isfalse. - Expecting
false false:String.equalscompares characters, not references, so two objects with the same text are equal.
- What does
a == bgive when both variables hold the literal"hi"? - How do you compare two strings while ignoring letter case?