Java Tutorials
this Keyword in Java: What is & How to use with Example
What is this Keyword in Java? this keyword in Java is a reference variable that refers to the...
compareTo() is used for comparing two strings lexicographically. Each character of both strings are converted into a Unicode value. However, if both the strings are equal, then this method returns 0 else it only result either negative or positive value.
The Java String compareTo() method is defined in interface java.lang.Comparable
Syntax: How to write a compareTo() method in Java:
public int compareTo(String str)
Parameter input :
str – The compareTo() function in Java accepts only one input String data type.
Method Returns:
This compareTo() Java method returns an int datatype which is based on the lexicographical comparison between two strings.
Java String compareTo() method Example:
Example 1:
public class Sample_String {
public static void main(String[] args) {
String str_Sample = "a";
System.out.println("Compare To 'a' b is : " + str_Sample.compareTo("b"));
str_Sample = "b";
System.out.println("Compare To 'b' a is : " + str_Sample.compareTo("a"));
str_Sample = "b";
System.out.println("Compare To 'b' b is : " + str_Sample.compareTo("b"));
}
}
Output
Compare To 'a' b is : -1
Compare To 'b' a is : 1
Compare To 'b' b is : 0
Here,
You can use method Use "compareToIgnoreCase" in case you don't want the result to be case sensitive. Let's understand with a Java compareTo() Example.
Example 2:
public class Sample_String {
public static void main(String[] args) {
//Compare to a String
String str_Sample = "RockStar";
System.out.println("Compare To 'ROCKSTAR': " + str_Sample.compareTo("rockstar"));
//Compare to - Ignore case
System.out.println("Compare To 'ROCKSTAR' - Case Ignored: " + str_Sample.compareToIgnoreCase("ROCKSTAR"));
}
}
Output
Compare To 'ROCKSTAR': -32
Compare To 'ROCKSTAR' - Case Ignored: 0
Java String compareTo() method is used to perform natural sorting on string. Natural sorting means the sort order which applies on the object, e.g., lexical order for String, numeric order for Sorting integers, etc.
Lexical order is nothing but alphabetically order. compareTo() Java method does a sequential comparison of letters in the string that have the same position.
In this method, if the first string is always lexicographically higher than second string, it returns a positive number.
if a1 > a2, it returns negative number
if a1 < a2, it returns positive number
if a1 == a2, it returns 0
Let's understand with Java String compareTo() Example.
Example 3:
public class Compare {
public static void main(String[] args) {
String s1 = "Guru1";
String s2 = "Guru2";
System.out.println("String 1: " + s1);
System.out.println("String 2: " + s2);
// Compare the two strings.
int S = s1.compareTo(s2);
// Show the results of the comparison.
if (S < 0) { System.out.println("\"" + s1 + "\"" + " is lexicographically higher than " + "\"" + s2 + "\"");
} else if (S == 0) {
System.out.println("\"" + s1 + "\"" + " is lexicographically equal to " + "\"" + s2 + "\"");
} else if (S > 0) {
System.out.println("\"" + s1 + "\"" + " is lexicographically less than " + "\"" + s2 + "\"");
}
}
}
Output:
String 1: Guru1
String 2: Guru2
"Guru1" is lexicographically higher than "Guru2"
What is this Keyword in Java? this keyword in Java is a reference variable that refers to the...
What is Command Line Argument in Java? Command Line Argument in Java is the information that is...
What is JDK? JDK is a software development environment used for making applets and Java...
What is Java Array? Java Array is a very common type of data structure which contains all the data...
JavaScript also abbreviated as JS, is a high level server side programming language. JavaScript is...
What is a Prime Number? A prime number is a number that is only divisible by 1 or itself. For...