The if keyword works with conditional statements to control the flow of a program. The conditional statements must be any expressions that return a boolean value.

There are various String expressions you can use in the conditional statement. One of these expressions is comparing String in Java. In this tutorial, you will learn how to compare a String with the Java if statement.

Using equals() method

The equals() method is a method from the String class that compares two Strings. Let’s look at an example to understand how this method works.

package com.javawhizz;
public class Main {
    public static void main(String[] args) {
        String name = "JavaWhizz";

        if (name.equals("JavaWhizz"))
            System.out.println(true);

        if ("JavaWhizz".equals(name))
            System.out.println(true);

    }
}

To compare a String to another String. Create a String and call the equals() method of the String. Next, pass the second String as the argument of the method.

As a result, the method will return true if the sequence of characters in the two String objects is the same. Otherwise, the method will return false.

As an alternative, you can also call the equals() method on the String literal and then pass the String to compare. This will have the same effect as the previous code.

Output

true
true

Using compareTo() method

The compareTo() method is available in the String class and you can also use it to compare two String objects. Here is an example showing how to use the method

package com.javawhizz;
public class Main {
    public static void main(String[] args) {
        String name = "JavaWhizz";

        if (name.compareTo("JavaWhizz") == 0)
            System.out.println(true);

        if ("JavaWhizz".compareTo(name) == 0)
            System.out.println(true);
        
    }
}

You call the method the same way you call the equals() method. The main difference is that the compareTo() method compares Strings in lexicographic order.

If the Strings are equal, the method returns 0. If the current String is less than the other String, the method returns a negative number. If the current String is greater than the other String, the method returns a positive number.

As an alternative, you can also call the method on the String literal and pass the other String.

Output

true
true

By comparing String objects

Java returns the same object if two String literals have the same sequence of characters. Note that this only works when using String literals but not String instances.

With this in mind, you can compare the two String literal objects using the == operator. If the sequence of characters is the same, then the objects will be the same. As a result, the result will be true. Otherwise, the result will be false. The following example shows how to use this approach.

package com.javawhizz;
public class Main {
    public static void main(String[] args) {
        String name = "JavaWhizz";

        String description = "JavaWhizz";

        boolean isNameEqualToDesc = name == description;

        System.out.println(isNameEqualToDesc);
    }
}

Output

true

Using intern() method

Java maintains a String pool for every String created in the program. You can use the intern() method to check for the availability of Strings in the String pool. As a result, you can compare Strings inside the pool using the == operator.

Let’s look at an example to understand how the method works.

package com.javawhizz;
public class Main {
    public static void main(String[] args) {
        String name = new String("JavaWhizz");
        String description = new String("JavaWhizz");

        boolean isNameEqualToDesc =
                name.intern() == description.intern();

        System.out.println(isNameEqualToDesc);
    }
}

Note that this approach is best suited when working with String instances. If the current String is already in the String pool, the intern method returns the String in the pool. Otherwise, the method adds the String to the pool and returns the object reference.

Since the String is already in the pool when the second intern() method is being called. This evaluates the expression to be true.

Output

true

Conclusion

In this tutorial, you have learned how to compare a String with the Java if statement. In conclusion, these are the common approaches used. Nonetheless, there are other approaches that you can leverage. In the meantime feel free to use any of the ones you have learned here.

You can also check the blog section of this site for more articles like this.

Happy Hacking!


0 Comments

Leave a Reply

Avatar placeholder

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