It would be an understatement to say that performing comparisons in programming is a common task. There are many types of comparisons that can be made, from boolean to arithmetics and even type comparisons. In some cases, you may need to confirm the length, size, or value of a piece of data, such as strings.

In this post, you will discover what Java String comparisons are, how to use them and what that means for you. You will also learn about the different types of comparisons focusing on equality comparisons. Finally, you will see some code examples of the syntax and use of comparisons for various tasks.

Without wasting any more time, let’s get to it.

Download Now: An Introduction to Java & JavaScript

How to Use Equals in Java

In Java, there are many ways to make comparisons, and when comparing information, the two things you must consider are the value and data type. This can inform your software planning and pseudocode much cleaner facilitating better coding practices.

There are, however, multiple ways to compare strings to each other, and they are the equals() method and the double equal operator. These approaches are meant to serve the same general purpose; to compare two strings to each other. However, each approach compares different aspects of the strings.

Because of the nature of Java Strings, there is more than one aspect of strings to keep in mind. For example, in Java, Strings are objects instead of primitives to compare a string’s value or type. You can even check if it is the same string instance as another one.

Let’s look at some examples of comparing string values and learn how it works.

How to Check if a String Equals Another String Java

With string values, you can use either approach to run a comparison between two different strings. However, there is a caveat to consider; the double equal operator will also compare the instance of the string. That is to say; it looks to see if the string is located in the same place in the Java Virtual Machine memory heap. This next video shows how the equals() method works and ways to make comparisons with it.

Let’s look at a code example that showcases how the equals() method compares string values.

 
public class Test {
  public static void main(String[] args)
  {
    String grtng1 = "HELLO";
    String grtng1Cpy = "HELLO";
    String grtng2 = "ALOHA";
    String grtngObjStr =  new String("HELLO");
    System.out.println(grtng1.equals(grtng1Cpy)); /*true*/
    System.out.println(grtng1.equals(grtng2)); /*false*/
    System.out.println(grtng1.equals(grtngObjStr)); /*true*/
  }
}

This code block creates multiple string variables and compares them using the equals() method. The first comparison returns true due to identical values, while the second compares different values and returns false. Finally, in the third comparison, the value of the two variables is identical, and as a result, it returns true.

The thing to understand here is that the equals() method only looks at the string's actual value, ignoring other aspects of the string. An example of an element it doesn’t look at is the memory location and string type. So next, let’s look at an example of string comparisons using the double equal operator.

Java String Equals vs. Equals-Equals

The double equals operator is a little different as it also looks at the memory location of the string as well. If the value and memory location are the same for the compared lines, then the condition returns true; otherwise, it returns false. The next video will elaborate on the difference between the way the two methods behave.

In this respect, no string created using the new keyword will return true compared to another string as it will have its memory location. Let’s look at this process and see what it looks like in the next section.

Java String Equals Example

The following code is almost identical to the previous code block with one primary difference. This time the comparisons are made using the double equal operator. As a result of this change, the comparison results are different.

 
class Test {
  public static void main(String[] args)
  {
    String grtng1 = "HELLO";
    String grtng1Cpy = "HELLO";
    String grtng2 = "ALOHA";
    String grtngObjStr =  new String("HELLO");
    System.out.println(grtng1 == grtng1Cpy); /*true*/
    System.out.println(grtng1 == grtng2 ); /*false*/
    System.out.println(grtng1 == grtngObjStr); /*false*/
  }
}

The first comparison is evaluated as identical, while the second and third, however, evaluate as different. This is because the double equal operator is also checking the memory location. For the comparison to return true, both the value and location need to pass the comparison.

Getting Started With String Comparisons

A lot has been covered in this post, and with this information, you are ready to start making string comparisons in your software code. There are still other ways to test, challenge, and build your knowledge and understanding of the string comparison process.

One final note to consider; the techniques discussed in this post can be used on other data types. This includes primitives and objects, and much more. You can continue making different kinds of comparisons to solidify your understanding of these techniques.

java

 javascript

Originally published May 24, 2022 7:00:00 AM, updated May 24 2022