Q1.What is the difference between == and equals() when comparing Strings?
Short answer: == checks whether two references point to the same object; equals() checks whether the text is the same. In tests, always compare expected and actual text with equals() (or an assertion library, which calls it for you).
String a = "Login";
String b = new String("Login");
System.out.println(a == b); // false - two different objects
System.out.println(a.equals(b)); // true - same characters
Common failure: driver.getTitle() == "Dashboard" fails even when the title looks right, because the browser returns a new String object.
- Using == for text from the browser or an API response
- Calling equals() on a value that may be null - write "Expected".equals(actual) or use Objects.equals
- What does equalsIgnoreCase do?
- Why do string literals sometimes pass an == check?
