Home  >  Article  >  Java  >  compareTo Java

compareTo Java

WBOY
WBOYOriginal
2024-08-30 15:08:42500browse

compareTo() is a method in Java that compares the string given with the current string in a lexicographical manner. Comparison is done on the basis of the Unicode value of characters available in the string.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Following are the different conditions in the compareTo() method.

  • If string 1 is lexicographically larger than string 2, a positive number will be returned.
  • If string 1 is lexicographically smaller than string 2, a negative number will be returned.
  • If string 1 is lexicographically equal to string 2, ‘0’will be returned.

Syntax:

Below is the syntax of compareTo() method:

public int compareTo(String s2)

Here, s2 is the string that is used for comparison with the current string. An integer value will be returned on calling this method.

How compareTo works in Java?

compareTo() method can be used in three ways.

  • compareTo(String st): Here, a comparison is done between strings. Suppose there are two strings s1 and s2. s1.compareTo(s2) method compares s1 and s2.
  • compareTo(Objectob): Here, a comparison is done between a string and an object ob. Suppose there are strings s1 and object ob.s1.compareTo(“Happy”) method compares s1 and string inside the argument.
  • compareToIgnoreCase(String s): compareTo method works similar to the first syntax, with the exception that this ignores the case. Suppose there are two strings s1 and s2. s1.compareToIgnoreCase(s2) method compares s1 and s2 without considering the case they are in.

Examples of compareTo Java

Given below are the examples of compareTo Java:

Example #1

Java program to implement compareTo method that compares two strings.

Code:

public class compareToExample {
//main method
public static void main(String args[])
{
//create three strings for comparison
String s1 = "Happiness lies within you";
String s2 = "Happiness LIES WITHIN YOU";
String s3 = "Happiness lies within you";
//store comparison result of s1 and s2 in v1
int V1 = s1.compareTo( s2 );
System.out.println( " Compare s1 and s2 : "+ V1 ) ;
//store comparison result of s1 and s3 in v2
int v2 = s1.compareTo( s3 );
System.out.println( " Compare s1 and s3 : "+ v2 ) ;
//store comparison result of s2 and s3 in v3
int v3 = s2.compareTo(s3);
System.out.println(" Compare s2 and s3 : "+ v3 ) ;
}}

Output:

compareTo Java

In this program, three strings are created s1, s2 and s3. Three variables, v1, v2 and v3, are also created for storing the comparison results of s1&s2, s1&s3, and s2&s3, respectively. It can be seen that a positive number is returned on comparing s1 & s2, and a negative number is returned on comparing s2 &s3. As both s1 and s3 are equal, 0 is returned in the second case.

Example #2

Java program to implement compareTo method that compares a string and an object.

Code:

public class compareToExample {
//main method
public static void main(String args[])
{
//create a string for comparison
String s1 = "Happiness lies within you";
//store comparison result of s1 and ARGUMENT in v2
int v2 = s1.compareTo( "Happiness LIES within you");
System.out.println( " Compare s1 and argument : "+ v2 ) ;
}
}

Output:

compareTo Java

In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareTo() method, and it can be seen that a positive number is returned on comparing s1 and argument.

Example #3

Java program to find the length of a string using the compareTo method.

Code:

public class compareToExample {
//main method
public static void main(String args[])
{
//create three strings for comparison
String s1 = "Happiness lies within you";
String s2 = "";
//length will be returned in positive
int V1 = s1.compareTo( s2 );
System.out.println( " Length of s1 : "+ V1 ) ;
//length will be returned in negative
int v2 = s2.compareTo( s1 );
System.out.println( " Length of s1 : "+ v2 ) ;
}
}

Output:

compareTo Java

In this program, two strings are created, s1 and s2, where s2 is a null string. If the given string is compared with a nullstring, then the length of the non-empty string will be returned. If a comparison is done in reverse order, a negative value of the length will be returned.

Example #4

Java program to implement compareToIgnoreCase method that compares two strings.

Code:

public class compareToExample {
//main method
public static void main(String args[])
{
//create three strings for comparison
String s1 = "Happiness lies within you";
String s2 = "Happiness LIES WITHIN YOU";
String s3 = "Happiness lies within you";
//store comparison result of s1 and s2 in v1
int V1 = s1.compareToIgnoreCase( s2 );
System.out.println( " Compare s1 and s2 : "+ V1 ) ;
//store comparison result of s1 and s3 in v2
int v2 = s1.compareTo( s3 );
System.out.println( " Compare s1 and s3 : "+ v2 ) ;
//store comparison result of s2 and s3 in v3
int v3 = s2.compareToIgnoreCase(s3);
System.out.println(" Compare s2 and s3 : "+ v3 ) ;
} }

Output:

compareTo Java

As already seen, compareToIgnoreCase ignores the case and compares the strings. As the three strings differ only in cases, 0 will be returned on calling this method.

Example #5

Java program to implement compareToIgnoreCase method that compares a string and an object.

Code:

public class compareToExample {
//main method
public static void main(String args[])
{
//create a string for comparison
String s1 = "Happiness lies within you";
//store comparison result of s1 and ARGUMENT in v2
int v2 = s1.compareToIgnoreCase( "Happiness LIES within you");
System.out.println( " Compare s1 and argument : "+ v2 ) ;
}
}

Output:

compareTo Java

In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareToIgnoreCase() method, and it can be seen that 0 is returned as the case is ignored.

Conclusion

compareTo() is a Java method that compares the string given with the current string in a lexicographical manner. In this article, different aspects such as syntax, working, and examples of the compareTo() method is seen in detail.

The above is the detailed content of compareTo Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java Micro EditionNext article:Java Micro Edition