String and == Puzzles
35 min · Intermediate
== on Strings asks "is this the same object?", while equals asks "is this the same text?". String puzzles are built to make those two answers differ, using the string pool, compile-time constants, immutable methods and wrapper caches.
Topics in this lesson
- Compile-time constant folding vs runtime concatenation
- final String variables and the pool
- replace, substring, toUpperCase without assignment
- Wrapper == puzzles mixed with Strings
- A checklist for any string puzzle
Interview questions this lesson answers
- For
String a = "Hi"; String b = new String("Hi");, what doa == banda.equals(b)give? - For
String c = "Hi"; String d = "Hi";, what doesc == dgive, and why? - Can
new String("Hi") == new String("Hi")ever be true? - What does System.out.println("Ja" + "va" == "Java") print?
- With
String s = "Ja";, what doess + "va" == "Java"give? - With
final String s = "Ja";, what doess + "va" == "Java"give? - Is every final String a compile-time constant?
- How many String objects does
String s = "a" + "b" + "c";create at run time? - What does
("Ja" + new String("va")).intern() == "Java"give? - What does
String s = "Hi"; String t = s; s += "!"; System.out.println(t);print?
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.