Java String codePoints() method



The Java String compareTo() method is used to compare two strings lexicographically. It returns an integer value, and the comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

  • The result is a negative integer if this String object lexicographically precedes the argument string.

  • The result is a positive integer if this String object lexicographically follows the argument string.

  • The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.

The compareTo() method accepts a parameter as a string, and it compares the strings lexicographically, the lexicographic compression means in the alphabetical order (a-z). It does not throw any exceptions while comparing the given strings.

Syntax

Following is the syntax of the Java String compareTo() method −

public int compareTo(String anotherString)

Parameters

  • anotherString − This is the String to be compared.

Return Value

  • This method returns the value 0, if the argument string is equal to this string.

  • This method returns negative value, if this string is lexicographically less than the string argument.

  • This method returns positive value, if this string is lexicographically greater than the string argument.

Example

If the given string values are the same, the compareTo() method returns zero.

In the following program, we are instantiating the two string class with the values “Hello” and “Hello” simultaneously. Then, using the compareTo() method, we are trying to check whether the given strings are lexicographically equal or not. Since, the given string is lexicographically equal to the string argument the method returns 0.

import java.lang.*;
public class Compare {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str1 = new String("Hello");
      String str2 = new String("Hello");
      System.out.println("The given strings are: " + str1 + " and " + str2);
      
      //using the compare two method
      System.out.println("The string " + str1 + " is lexicographically equal to the string " + str2 + " or not? " + str1.compareTo(str2));
   }
}

Output

On executing the above program, it will produce the following result −

The given strings are: Hello and Hello
The string Hello is lexicographically equal to the string Hello or not? 0

Example

If the string is lexicographically greater than the string argument, this method returns a positive value.

In this example, we are creating two string literals with the values “Tutorials” and “Point” simultaneously. Using the toCompare() method, we are trying the check whether the given strings are lexicographically equal or not. Since, the given string is lexicographically greater than the string argument the method returns the positive value.

import java.lang.*;
public class Compare {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str1 = "Tutorials";
      String str2 = "Point";
      System.out.println("The given strings are: " + str1 + " and " + str2);
      
      //using the compare two method
      int compare = str1.compareTo(str2);
      System.out.println("The compareTo() method returns: " + compare);
      if (compare == 0) {
         System.out.println("The strings are lexicographically equal");
      } else if (compare > 0) {
         System.out.println("The first string is larger than the second string lexicographically.");
      } else {
         System.out.println("The first string is smaller than the second string lexicographically.");
      }
   }
}

Output

Following is the output of the above program −

The given strings are: Tutorials and Point
The compareTo() method returns: 4
The first string is larger than the second string lexicographically.

Example

If the string is equal to the string argument, but the argument string is in a different case, the compareTo() method returns the positive value.

In the following example, we are instantiating the string class with the value “hello” and “HELLO”. Using the compareTo() method, we are trying to compare strings lexicographically.

package com.tutorialspoint.String;
import java.lang.*;
public class Compare {
   public static void main(String[] args) {
      
      // instantiating the String class
      String str1 = new String("hello");// string
      String str2 = new String("HELLO");// string argument
      System.out.println("The given strings are: " + str1 + " and " + str2);
      
      //using the compareTo() method
      int val = str1.compareTo(str2);
      System.out.println("The str1.compareTo(str2) value is: " + val);
      if(val == 0) {
         System.out.println("The string " + str1 +  " and string argument " + str2 + " are lexicographically equal");
      } else if(val > 0) {
         System.out.println("The string " + str1 +  " is lexicographically greater than the string argument " + str2);
      } else {
         System.out.println("The string " + str1 +  " is lexicographically less than the string argument " + str2);
      }
   }
} 

Output

After executing the program, it will return the following output −

The given strings are: hello and HELLO
The str1.compareTo(str2) value is: 32
The string hello is lexicographically greater than the string argument HELLO
java_lang_string.htm
Advertisements