4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Sometimes, we want to compare objects to see if they are equal. However, using the boolean equality operator may get some unexpected results!
When using the equality operator == to compare two objects, it will only return true if they refer to the same exact object... not just a copy of that object, but that object itself. Even if the objects have the same exact attributes but are two different instantiations, the equality operator will return false (like identical twins, even though they may look the same, they are still two different people).
Here are some examples with String objects:
String a = "Hi"; String b = "Hi"; String c = a; String d = "Hi!"; String e = new String("Hi"); System.out.println(a == c); System.out.println(d == b); System.out.println(a == b); System.out.println(a == e);
true false true false
In this case, the variable c is used to point to the string at a. That is, c is just another name for a. This means that they are the same string, so the equality to return true.
The text in string d is not the same as the text in string b. Because they are different, they are different strings. Because the strings are different, the equality returns false.
This one is simple. String a is "Hi" while String b is also "Hi", which is the same. Since the String constructor is not called, a and b are the same string, so the equality returns true.
Even though both strings are initialized to "Hi", e uses the String constructor, so the constructor causes e to be a different string from a. Because of this, the equality operator returns false. However, this should return true in most practical situations because in most cases it doesn't matter if they are different strings as long as they say "Hi". This is where the equals() method comes into play.
Often, we want to check if two objects have the same characteristics, such as two different String objects saying the same text. This is when the equals() method comes in. This comes with all classes in Java (we'll learn about this mysterious Object class in Unit 9!). Unlike the boolean equality operator, the equals operator for most classes returns true if their attributes are the same, even though they aren't the same object (like identical twins!).
We use it with the following syntax, where objectOne is being compared to objectTwo:
objectOne.equals(objectTwo);
Note that the order doesn't matter and one can write this with the same result:
objectTwo.equals(objectOne);
Here is an example illustrating the use of equals():
String a = "Hi"; String b = "Hi"; String c = a; String d = "Hi!" String e = new String("Hi"); System.out.println(a.equals(c)); System.out.println(d.equals(b)); System.out.println(a.equals(b)); System.out.println(a.equals(e));
true false true true
In this case, the variable c is used to point to the string at a. That is, c is just another name for a. This means that they are the same string, so the method will return true. This is the same as using the "==" operator.
The text in string d is not the same as the text in string b. Because they are different, they are different strings. This will cause the method to return false, unlike the first case with a and c. Like the last case, this is the same as using the "==" operator.
This one is simple. String a is "Hi" while String b is also "Hi", which is the same. Since the String constructor is not called, a and b are the same string. Moreover, they have the same sequence of characters, so the method returns true, the same as when we used the "==" operator.
This one is the one that is different than with the "==" operator. Even though e uses the String constructor, the value that the strings are initialized to are both "Hi", so the method returns true, even though the constructor causes e to be a different string from a.
However, note that the equals method can only be used with objects and not primitive types! For primitive types, we simply need to use the boolean equality operator.
When you're comparing strings and other objects, you almost always want to use the equals method. The only reason you should use the "==" operator is if a string or object has to be the exact same string or object you're checking against.
The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.
Term 1 of 2
The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.
Term 1 of 2
The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.
Term 1 of 2
The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.
== (double equal sign): The double equal sign is another way to compare objects for equality, but it compares their memory addresses instead of their values.
String: A String is a sequence of characters. In Java, you can use equals() to compare two String objects and check if they have the same content.
compareTo(): The compareTo() method is used to compare two objects based on their natural ordering. It returns an integer value that indicates whether one object is less than, equal to, or greater than another object.
Primitive types are basic data types in programming that are built-in and cannot be broken down into smaller components. They represent simple values like numbers, characters, and boolean values.
Variables: Variables are used to store values in programming. They can hold different primitive types or other data structures.
Data Types: Data types categorize the kind of value that a variable can hold. Primitive types are one category of data types.
Operators: Operators perform operations on variables or values. Different operators work differently depending on the primitive type being used.