O'Reilly logo

Special Edition Using Java 2 Standard Edition by Geoff Friesen, Chuck Cavaness, Brian Keeton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required