Compare String With the Java if Statement

Haider Ali Jan 30, 2023 Nov 22, 2021
  1. Compare String With the Java if Statement Using the == Operator
  2. Compare String With the Java if Statement Using the equal() Function
  3. Compare String With the Java if Statement Using the compareTo() Function
Compare String With the Java if Statement

In this guide, we are going to talk about if statement string comparison in Java. There are generally three ways to compare two strings. You need to understand the basics of these operations and find out what you are comparing (content, reference, or string difference). Let’s take a deeper look into this.

Compare String With the Java if Statement Using the == Operator

When we compare two strings through the if statement using the == operator, we compare the reference number of those strings, but you’ll notice that it’ll work the same as comparing the content. If there are two strings with the same content, it’ll show them as equal. Why? Because the compiler of Java is mature enough to store the two strings with the same content in the same memory.

Compare String With the Java if Statement Using the equal() Function

Through the equal() function, we can compare the content of the two strings. It will see if the content is similar. It’s case sensitive, but you can also ignore the case sensitivity by using the equalsIgnoreCase() function instead.

Compare String With the Java if Statement Using the compareTo() Function

In this function, we get the difference between two strings. We compare them lexicographically based on each character’s Unicode value. You will get a 0 value if both the strings are equal, and you will get less than the 0 value if the string is less than the other string and vice versa.

Take a look at the following self-explanatory code.

public class Main {
    public static void main(String[] args) 
    {

        String str1 = "jeff";
        String str2 = "jeff";
        String str3 = new String("jeff"); // to declare
        String str10 = new String("jeff");
        System.out.println("-----------------Using == Operator ----------------");
        // using == opreater use for Refrence Comapring instead of content comparison.
        if (str1 == str2) 
        { // equal and if Conditon True because both have same Refrence Memory address.
            System.out.println("Str1 And Str2 Equal");
        }
        if (str1 == str3) 
        { // Not Equal If Condition False Because == opreater compares objects refrence.
            System.out.println("Str1 and Str3 are equals");
        }
        if (str10 == str3) 
        { // Not Equal If Condition False Because == opreater compares objects refrence.
            System.out.println("Str10 and Str3 are equals");
        }

        System.out.println("-----------------Using .equal Method----------------");
        // Using .equals Method. for String Content Comparison.

        if (str1.equals(str2)) 
        { // equal and if Conditon True because both have same string
            System.out.println("Str1 And Str2 Equal");
        }
        if (str1.equals(str3)) 
        { // Equal If Condition true String have same Content.
            System.out.println("Str1 and Str3 are equals");
        }
        // compare two strings diffrence
        System.out.println("-----------------Using Compare Method----------------");
        // first string.toCompare(String2)

        System.out.println(str1.compareTo(str2));

    }

}

Output:

Output:
-----------------Using == Operator ----------------
Str1 And Str2 Equal
-----------------Using .equal Method----------------
Str1 And Str2 Equal
Str1 and Str3 are equals
-----------------Using Compare Method----------------
0
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java String