Comparing Strings
Often, you need to know when two strings are equal. For example, you might want to compare a string entered by the user to another string in your program or compare data returned by the database for a particular value. There are three general ways you can compare strings:
Calling the equals() method
Calling the equalsIgnoreCase() method
Using the normal comparison operator ==
The equals method returns true when the argument is non-null and the argument string represents the same set of characters as the source string. Otherwise, it returns false. Here's an example:
String str = "This is a string"; boolean result = str.equals("This is a string");
Here, the boolean variable result is equal to true. Because strings of exactly ...
Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.