Java String compareTo example

Previous
Next
String’s compareTo method compares two String lexicographically. Both String get converted to unicode value and then compares.

If you call str1.compareTo(str2)

then if it returns
positive number : str1 is greater than str2
0: str1 is equal to str2
negative number : str1 is smaller than str2

Java String compareTo example

When you run above program, you will get below output:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog