Java String compareTo() Function Tutorial with Examples – POFTUT

Java String compareTo() Function Tutorial with Examples

Java programming language provides the compareTo() function which is used to compare two strings. The comparison is done letter by letter and according to the situation, some results are returned by the compareTo()function. The comparison is done with the Unicode character set.

compareTo() Syntax

First, we will learn the syntax of the compareTo() function as the return type, parameters, etc. compareTo() function has two main syntaxes where one accepts a string parameter and the other accepts an object type parameter.

  • Here the return type fo the compareTo() function is int which will provide the comparison result as an integer.
  • There is an only a single string which will be compared with the compareTo() function object like text.compareTo(myword)

  • Here the return type fo the compareTo() function is int which will provide the comparison result as an integer.
  • There is only a single object type parameter which will be converted to the string and compared with the compareTo() function like myobj.compareTo(yourobj)

compareTo() Return Values

The most important part of the compareTo() function is the returned result. As we have learned compareTo() function returns an int or integer type value which express the comparison result. Let’s look following code which result will be like below.

  • If result is 0 both strings are the same
  • If result is bigger than 0 myfirststring is bigger than mysecondstring
  • If result is lower than 0 mysecondstring is bigger than myfirststring

Compare with Strings

Let’s work with the compareTo() function with different strings in examples. In these examples, we will compare the strings ABC , ABD , ACD with each other.

LEARN MORE  How To Convert String To Int (Integer) In Java?

Compare with Strings

Compare with Strings

We will compile with the  javac and run the Java byte code like below.

compareTo() Function Is Case Sensitive

While comparing the string case sensitivity is very important. Case sensitive comparison will result different for same letter upper case and lower case like A and a. compareTo() function is also case sensitive by default because each letter has a different number for Unicode. We can use compareToIgnoreCase() function if we want to compare in case-sensitively.

LEARN MORE  What Is String Data Type In JavaScript, Java, Python, C# , PHP, C, C++, PowerShell Programming Languages?

compareTo() Function Is Case Sensitive

compareTo() Function Is Case Sensitive

compareToIgnoreCase() Function

If we want to compare two strings as in case sensitive or by ignoring case sensitivity we can use comopareToIgnoreCase() function like below. The syntax and usage are the same as the compareTo() function.

compareToIgnoreCase() Function

compareToIgnoreCase() Function

compareToIgnoreCase() Function

compareToIgnoreCase() Function

As we can see all string compare operations provided which means equal with the case insensitive comparison.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)