How To Compare Strings in Java

comparing string in Java

In Java, a String is a sequence of characters. It is a reference type, meaning it is an object rather than a primitive data type. Strings are immutable, which means that their values cannot be changed after they are created.


There are several ways to compare two strings in Java and we are going to discuss three methods out of them.


1. Using the equals() method: This method compares the contents of the strings and returns a boolean value. For strings, the equals() method is used to compare the characters of two strings to check if they are equal or not.

Java Example Code:

class StringComp {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "World";

        if(str1.equals(str2)) {
            System.out.println("str1 is equal to str2");
        }
        else {
            System.out.println("str1 is not equal to str2");
        }

        if(str1.equals(str3)) {
            System.out.println("str1 is equal to str3");
        }
        else {
           System.out.println("str1 is not equal to str3");
        }

    }
}
Output:
str1 is equal to str2
str1 is not equal to str3


2. Using the compareTo() method: This method compares the lexicographical ordering of the strings and returns an integer value. It returns 0 if the strings are equal, a negative value if the first string is lexicographically less than the second, and a positive value if the first string is lexicographically greater than the second.

Java Example Code:
//Java code to compare two strings
class StringComp {
    public static void main(String[] args) {
        String str1 = "Apple";
        String str2 = "Banana";
        int result = str1.compareTo(str2);
        if (result == 0) {
            System.out.println("Strings are equal");
        } else if (result < 0) {
            System.out.println("String 1 is less than String 2");
        } else {
            System.out.println("String 1 is greater than String 2");
        }
    }
}
Output:
String 1 is less than String 2


3. Using the equalsIgnoreCase() method: The equalsIgnoreCase() method in Java is used to compare two strings for equality, ignoring the case of the characters. In other words, this method compares two strings by ignoring their case and returns true if they are equal or false if they are not.

Java Example Code:
//Java code to compare two with case strings
class StringComp {
    public static void main(String[] args) {
        String str1 = "HELLO";
        String str2 = "hello";
        if (str1.equalsIgnoreCase(str2)) {
           System.out.println("Strings are equal");
    }
}
Output:
Strings are equal


Why we should not use == for String Comparison in Java?

In Java, the == operator compares the reference or memory address of two objects, not their actual values. When comparing strings, it is necessary to compare their actual values, not just their memory addresses. Therefore, using the == operator to compare two string variables may lead to unexpected results.

Java Example Code:
class StringCompare {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = new String("Hello");

        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false
    }
}
Output:
true
false

In the above example, str1 and str2 both refer to the same memory address since they are assigned the same string literal value. Therefore, the == operator returns true for their comparison. However, str3 is a new String object with a different memory address, even though it contains the same value as str1. Hence, the == operator returns false for the comparison of str1 and str3.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS