How to compare 2 string in java Program

public class LastIndexOfExample {
  public static void main(String args[]) {
    String s1 = "hello";
    String s2 = "hello";
    String s3 = "meklo";
    String s4 = "hemlo";
    System.out.println(s1.compareTo(s2));
    System.out.println(s1.compareTo(s3));
    System.out.println(s1.compareTo(s4));
  }
}

Here’s a step-by-step explanation of the program:

1. Class and Method Declaration:

public class LastIndexOfExample

{

    public static void main(String args[])

  • The program declares a class named LastIndexOfExample.
  • The main method is the entry point of the program, where execution begins.

2. String Declarations:

String s1=”hello”;

String s2=”hello”;

String s3=”meklo”;

String s4=”hemlo”;

Four String variables are declared:

  • s1 is assigned the string “hello”.
  • s2 is assigned the string “hello”.
  • s3 is assigned the string “meklo”.
  • s4 is assigned the string “hemlo”.

3. compareTo() Method Calls:

System.out.println(s1.compareTo(s2));

System.out.println(s1.compareTo(s3));

System.out.println(s1.compareTo(s4));

compareTo() Method: The compareTo() method compares two strings according to lexicographical order, and returns:

  • 0, if the two strings are the same.
  • A positive value when the first string is lexicographically greater than the second one.
  • A negative value when the first string is lexicographically smaller than the second one.

And now, let’s work through each of the comparisons:

1. s1.compareTo(s2)

  • s1 is  “hello”  and s2 is also  “hello”.
  • Since both the strings are the same, s1.compareTo(s2) will return 0

  .

2. s1.compareTo(s3)

  • s1 is  “hello”  and s3 is  “meklo” .
  • s1: the first character is ‘h’, and its Unicode is 104. For s3, the first character is ‘m’, whose Unicode is 109.
  • In the comparison of s1.compareTo(s3), since lexicographically, ‘h’ appears before ‘m’, then s1.compareTo(s3) would return a value of negation; it would actually be -7, since there is a -7 difference of Unicode values, that between ‘h’, 104 and between ‘m’, 109.

 Now let’s work on. 

3. s1.compareTo(s4)

  • s1 is “hello” and s4 is “hemlo”.
  • The first two characters ‘h’ in both strings are the same.
  • The third character in s1 is ‘l’ (Unicode value 108) and in s4, it is ‘m’ (Unicode value 109).
  • Because ‘l’ is lexicographically less than ‘m’, s1.compareTo(s4) will return a negative value. It will return -1, because the Unicode difference between ‘l’ (108) and ‘m’ (109) is -1.

4. Output:

The System.out.println() statements will print the result of each comparison:

System.out.println(s1.compareTo(s2));

System.out.println(s1.compareTo(s3)); 

System.out.println(s1.compareTo(s4)); 

Expected Output:

0

-7

-1

Summary:

  • s1.compareTo(s2) returns 0 because the strings “hello” and “hello” are equal.
  • s1.compareTo(s3) returns  -7; since “”hello”” is lexicographically smaller than “”meklo”” by the first differing character.
  • s1.compareTo(s4) returns  -1; because “”hello”” is lexicographically smaller than “”hemlo”” by the third differing character.

Leave a Comment

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