Java Object equals() Method

Java Object equals() Method

String comparison is a fundamental operation in Java, often used in situations like user authentication, database queries, and text processing. While == checks for reference equality and .equals() compares actual content, Java also provides Objects.equals() as an alternative method.

In this blog, we will learn how we can compare two strings with the help of the Objects.equals() method in Java.

Table of Contents:

What is Objects.equals() Method in Java?

The Objects.equals() method in Java is a utility method from the java.util.Objects class. It is used to compare two strings (or any objects), handling null values safely. It returns true if both objects are equal or both are null, otherwise false.

Syntax:

boolean result = Objects.equals(string1, string2);

Where:

  • string1 is the first string to compare
  • string2 is the second string to compare
  • result is the boolean value that is either true or false.

Examples of Objects.equals() Method to Compare Strings in Java

Here are the following methods to compare strings in Java using Objects.equals():

Example 1: Comparing strings in Java

In the following example, we will compare two strings in Java, also we will handle null values as well:

Code:

Java

Output:

Comparing strings in Java

Explanation: In the above code, str1 and str2 have the same value, so Objects.equals(str1, str2) returns true. However, str1 and str3 are not equal because str3 is null, so Objects. equals(str1, str3) returns false. Finally, since str3 is null, comparing it with null returns true

Example 2:  Comparing User Input Strings

Let’s take an example of comparing two inputs taken from the user.

Code:

Java

Output:

Comparing User Input Strings

Explanation: This program takes two strings as input from the user and compares them using Objects.equals(). This method ensures a null-safe comparison, meaning it won’t throw a NullPointerException even if one of the inputs is null

Conclusion

In this blog, we have learned to compare two strings using the Objects.equals() method in Java. This method is used to safely compare two strings while handling `null` values without throwing a `NullPointerException`. Unlike the `==` operator, which checks reference equality, and the `.equals()` method, which requires explicit null handling, `Objects.equals()` provides a more robust and concise way to compare strings in Java.

If you want to learn more about Java, you may refer to our Java Course.

Some Other Methods to Compare Strings in Java

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner