Compare Strings

When comparing strings in Java you can either use the compareTo() method which the String class implements through the Comparable interface, or the equals() method which is declared in the Object class.
Since the String class is derived from the Object class (like every class is) any of the methods can be used when working with strings in Java.
In this code example we use the equals() method. Note that the method is case sensitive so if you want to make a case insensitive comparison you’ll need to use the method equalsIgnoreCase().

The output from the code above is:

Search for more Java info on this site here:
Custom Search

Please type any questions here.