1. Technology

Comparing Strings


There will come a time when you'll want to know if one String is the same as another String. The important thing to remember is a String is not a primitive data type. For example, with ints you can just use a double equals sign:

int ten = 10;
if (ten == 10){

This does not work with Strings because they are objects. If you do the following:

String firstString = "Happy";
String secondString = "Happy";
if (firstString == secondString){

what you are actually testing is whether the objects live at the same location. It might be the case the Strings are pooled in the same place and the result returns true but don't rely on it. A piece of code like like this:

String happyString = "Happy";
String birthdayString = "Birthday";
String firstString = happyString + " " + birthdayString;
String secondString = happyString + " " + birthdayString;
System.out.println(firstString == secondString);
will show you that although the String values are the same the locations are different. This means the equality operator will return a false value.

So, to compare the String values of two strings use the equals method of the String class:


The equals method can also be used with String literals:

"Happy Birthday".equals(firstString);

As you might expect with Java when the equals method is used the case of the String value is taken into account. If you don't want that to happen (i.e., you want "Happy Birthday" to equal "happy birthday") then use the equalsIgnoreCase method:

"happy birthday".equalsIgnoreCase(firstString)

Other Useful String Comparisons

The String class also provides other methods for partially comparing Strings. The startsWith method allows you to find out if a String value starts with a specified String. For example, to find out if a String starts with "Happy":


and you can add an index value if you want to skip over a few characters at the start of a String. The index is based on the position of the character you want to start comparing from. The index starts at zero. For example, the firstString variable each character would have the following index values: H is 0, a is 1, p is 2, p, is 3, y is 4. So, we could look for a String "appy" and start at index 1:


Similarly there is an endsWith method. This can be useful method if you wanted to check the suffix of a filename:

String filename = "picture.jpg";
if (filename.endsWith(".jpg")){

It can be handy if you have a few files and you want to process them on their file type.

You can also compare strings lexicographically by using the compareTo method. This uses the Unicode codepoint values of the characters in each String. If the String precedes the String it's compared to then the returned value is less than 0. If the two Strings are the same it returns a 0. And if the String follows the String it's compared to then the value is more than 0. For example, if we compare "Apple" with "Pear":

String firstString = "Apple";
String secondString = "Pear";

if (firstString.compareTo(secondString) < 0){

we get a value less than zero because "Apple" precedes "Pear".

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Understanding Data Types
  6. Java Syntax: Comparing Strings

©2014 About.com. All rights reserved.