Configuration

Night mode

CompareTo

Introduction To Java Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

String Methods

CompareTo

The compareTo() method compares the first letter of one String object to the first letter of another String object and returns a number that states which String comes first in lexicographic order (A,B,C etc.). If the first two letters of the strings are the same, the method will go on to compare the next letters, and the next until it finds the first instance of a difference. Let's take a look at how this method works.

The compareTo() method returns 0 if the two strings are equal, a number less than 0 if the first String is larger, and a number greater than 0 if the second String is larger. Uppercase letters come before lowercase letters when the method compares strings. As you work through learning this method's function, test our example program and use the ascii table to see how far apart the characters are from each other.