Workbook 1c, p.101 — How to correctly compare Strings in Java
.equalsIgnoreCase() when capitalization should not matter== checks if two variables point to the exact same object in memory. With Scanner input, Java creates a new object even if the text matches — so == returns false.
.equals() compares the content of both strings — the actual characters. It works correctly regardless of how the string was created.
== on Strings from Scanner input will return false even when the text matches. Always use .equals().
"Add" matches "Add""add""Add" matches "add""ADD"String a = "Hello" and String b = "Hello", both variables point to the same object — and == returns true.
Scanner, Java creates a brand new object. Even if the user types "Hello", it is a different object from the literal "Hello" in your code. So == returns false even though the characters are identical.
== is a trap — it works in quick tests but breaks in real programs. Always use .equals().
We will explore objects and memory in more detail in a later week.