How to compare two strings in java which is greater

JavaObject Oriented ProgrammingProgramming

Compare two strings using compareTo() method in Java. The syntax is as follows −

int compareTo(Object o)

Here, o is the object to be compared.

The return value is 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.

Example

Let us now see an example −

 Live Demo

public class Demo {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String str2 = new String("Strings are immutable");       String str3 = new String("Integers are not immutable");       int result = str1.compareTo( str2 );       System.out.println(result);       result = str2.compareTo( str3 );       System.out.println(result);    } }

Output

0 10

Let us see another example wherein we are comparing two strings lexicographically, ignoring case differences using compareToIgnoreCase(). This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

The syntax is as follows −

int compareToIgnoreCase(String str)

Here, str is the string to be compared.

Example

Let us now see an example to compare strings, ignoring case −

 Live Demo

public class Demo {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String str2 = "Strings are immutable";       String str3 = "Integers are not immutable";       int result = str1.compareToIgnoreCase( str2 );       System.out.println(result);       result = str2.compareToIgnoreCase( str3 );       System.out.println(result);       result = str3.compareToIgnoreCase( str1 );       System.out.println(result);    } }

Output

0 10 -10

How to compare two strings in java which is greater

Updated on 26-Sep-2019 13:23:44

It is easier to understand the comparison of characters before learning the comparison of string literals. A comparison of strings is given below this introduction. With Java, characters are represented in the computer by integers (whole numbers). Comparing characters means comparing their corresponding numbers.

With Java, uppercase A to uppercase Z are the integers from 65 to 90. A is 65, B is 66, C is 67, until Z, which is 90. Lowercase ‘a’ to lowercase ‘z’ are the integers from 97 to 122. ‘a’ is 97, ‘b’ is 98, ‘c’ is 99, until ‘z,’ which is 122. Decimal digits are the integers, 48 to 57. That is, ‘0’ is 48, ‘1’ is 49, ‘2’ is 50, until 9, which is 57.

So, in this new order, digits come first before uppercase letters, which come next before lowercase letters. Before the digits, there is the bell, which is a sounding and not a printable character. Its number is 7. There is the tab character of the keyboard, whose number is 9. There is the newline character (pressing the Enter key), whose number is 10. There is the space character (pressing the space-bar key), whose number is 32. There is the exclamation character, whose number is 33. There is the forward-slash character, whose number is 47. ‘(’ has the number, 40 and ‘)’ has the number, 41.

The sequence for characters is made up of some non-alphanumeric characters, whose numbers are below 48; the numbers for decimal digits come next; some non-alphanumeric characters are in the next interval; numbers for uppercase letters follow; some non-alphanumeric characters are in the next interval; the numbers for lowercase letters follow; after this, there are numbers for other non-alphanumeric characters of the English language.

These numbers are called code numbers. The code number for $ is 36. The code number for % is 37. The code number for & is 38. The code number for [ is 91. The code number for the backslash \ is 92. The code number for ] is 93. The code number for { is 123. The code number for | is 124. The code number for } is 125.

So, characters form a new kind of alphabet with its own ordering. The ordering is simple: just use the code numbers. That is, the sequence is simple: just use the code numbers and be able to associate each code number to its character. Do not forget that decimal digit code numbers come before uppercase letters, and uppercase letters come before lowercase letters.

And so, ‘A’ is less than ‘a’, written as, ‘A’ < 'a'. '0' is less than 'A', written as, '0' < 'A'. '%' is less than '0', written as, '%' < '0'.

Comparing characters in Java means comparing their equivalent code numbers. Comparing characters in Java uses relational operators, which are:

< meaning, less than > meaning, greater than <= meaning, less than or equal to >= meaning, greater than or equal to == meaning, equal to

!= meaning, not equal to

Java does not use these operators to compare string literals. Java uses different methods for string comparison – see below. In fact, == means something else for string comparison – see below. The equivalent of the above operators, for strings, is explained below.

With characters in Java, there is a new dictionary of symbols. Each symbol is represented internally by a number(integer). In Java, a string literal does not consist of only alphanumeric characters. In Java, a string literal is a sequence of alphanumeric characters mixed with other characters. Comparison of string literals in Java has to consider this new dictionary of symbols (characters). The comparison is done in the same way as with the normal dictionary. In comparing string literals in Java, “$textA[25]” < "$textB[26]", but coded differently – see below.

Article Content

Constructing a String

A string can be constructed in the Java computer language, as in the following examples:

String str = "What? $10,000!";

String str = new String("What? $10,000!");

char ch[] = {'W','h','a', 't', '?', ' ', '$', '1', '0', ',', '0', '0', '0', '!'};


String str = new String(ch);

Already in this short string literal, four non-alphanumeric characters (‘?’, ‘$’, ‘,’, ‘!’) can be found. It is not uncommon to also find the characters, ‘(’, ‘)’, ‘&’, ‘%’, ‘{‘, and ‘}’ in strings produced by ordinary computer users. In comparing string literals, they take their positions in the sequence of numbers of the character “dictionary,” based on numbers.

There is a difference between a string object and a string literal. A string object is the instantiation of the string class. A string literal is a text in question. In the above examples, str is a string object, and “What? $10,000!” without the quotes is a string literal.

The String Equals Method

The syntax is:

boolean equals (String s)

It returns true if the string literals are equal, in normal dictionary fashion; and false otherwise. This comparison is case sensitive. Consider the following code:

String str1 = "$textA[26]";
String str2 = "$textA[26]";
boolean blA = str1.equals(str2);
System.out.println(blA);  

String str3 = "$textA[26]";


String str4 = "$TEXTA[26]";
boolean blB = str3.equals(str4);
System.out.println(blB);

The output is:

true
false

The sequences are the same in comparing str1 and str2 literals, so the return value is true. In comparing str3 and str4, the sequences are the same, but one string literal has lowercase text, and the other has uppercase text, so the return value is false.

boolean equalsIgnoreCase (String s)

With this method, the casing is ignored. If the sequences are the same, if one literal is in lowercase and the other is in uppercase, this method will return true. The output of the following code is:

true
true

Code:

String str1 = "$textA[26]";
String str2 = "$textA[26]";
boolean blA = str1.equalsIgnoreCase(str2);
System.out.println(blA);  

String str3 = "$textA[26]";


String str4 = "$TEXTA[26]";
boolean blB = str3.equalsIgnoreCase(str4);
System.out.println(blB);

Problem with == for Strings

== compares the references of two string objects. It is not used to compare two string literals.

Less Than, Greater Than

int compareTo (String s)

This method returns a negative integer, if the left string literal is less than the right string literal. It returns 0 if both string literals are equal. It returns an integer greater than 0 if the left string literal is greater than the right string literal. The following code returns, -32 in the author’s computer:

String str1 = "ABCD";
String str2 = "abcd";
int it = str1.compareTo(str2);
System.out.println(it);

The following code returns -4 in the author’s computer:

String str1 = "abcd";
String str2 = "efgh";
int it = str1.compareTo(str2);
System.out.println(it);

The following code returns 0 in the author’s computer:

String str1 = "abcd";
String str2 = "abcd";
int it = str1.compareTo(str2);
System.out.println(it);

The following code returns +4 in the author’s computer:

String str1 = "efg";
String str2 = "abcd";
int it = str1.compareTo(str2);
System.out.println(it);

int compareToIgnoreCase (String s)

This method is the same as compareTo(), but case is ignored. The following code returns 0 in the author’s computer:

String str1 = "ABCD";
String str2 = "abcd";
int it = str1.compareToIgnoreCase(str2);
System.out.println(it);

The following code returns 0 in the author’s computer:

String str1 = "A2C3";
String str2 = "a2c3";
int it = str1.compareToIgnoreCase(str2);
System.out.println(it);

Less Than or Equal To

In order to do less-than-or-equal-to for string literals, two different methods have to be combined in an if-condition, as in the following code sample:

String str1 = "ABCD";
String str2 = "abcd";

if (str1.compareToIgnoreCase(str2)==0 && str1.compareTo(str2)<0)


    System.out.println("Left literal is less than or equal to right literal.");

The output is:

Left literal is less than or equal to right literal.

Greater Than or Equal To

The following code illustrates the situation for greater-than-or-equal-to:

String str1 = "abcd";
String str2 = "ABCD";

if (str1.compareToIgnoreCase(str2)==0 && str1.compareTo(str2)>0)


    System.out.println("Left literal is greater than or equal to right literal.");

The output is:

Left literal is greater than or equal to right literal.

Conclusion

In Java, all characters are represented by integers. These integers are the code numbers for the characters. Uppercase letters are represented by integers that are smaller than the integers for lowercase letters. All these code numbers exist in a sequence from smallest to biggest. A comparison of characters uses this sequence. Comparing string literals also uses this sequence similarly to how words of the normal dictionary are compared. Comparing characters uses arithmetic relational operators. Comparing of string literals do not use arithmetic relational operators. It uses certain Java string methods, which have been explained above.

There is a difference between a string object and a string literal. The focus in this tutorial has been on the comparison of string literals. A string object is an instantiation of the string class. A string literal is a sequence of characters in double-quotes. == can be used to test the equality of single characters, but not the equality of string literals. With string objects, == tests the equality of string object references.

Note: “A” is a string, while ‘A’ is a character.

Chrys

How to compare two strings in java which is greater

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.