Objects.equals()
is a method that compares objects. It uses Object.equals()
internally, and the patterns we have implemented so far when comparing objects are just made into a util class.
Lets see how to use
Objects.equals()` and examples.
Source
equals()
of the Objects class is implemented as follows.
package java.util;
public final class Objects {
/**
* Returns {@code true} if the arguments are equal to each other
* and {@code false} otherwise.
* Consequently, if both arguments are {@code null}, {@code true}
* is returned and if exactly one argument is {@code null}, {@code
* false} is returned. Otherwise, equality is determined by using
* the {@link Object#equals equals} method of the first
* argument.
*
* @param a an object
* @param b an object to be compared with {@code a} for equality
* @return {@code true} if the arguments are equal to each other
* and {@code false} otherwise
* @see Object#equals(Object)
*/
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
}
Even if you did not use the Objects class, it seems that everyone has experience using it as follows.
Since the object calling equals()
could be null, we had to throw a null exception.
String aa= "android";
String bb = "google";
if (aa.equals(bb)) {
System.out.println("aa equal to bb");
}
if (aa != null && aa.equals(bb)) {
System.out.println("aa equal to bb");
}
The Objects
class is just a util class that was implemented directly as above.
As you can see from the Objects.equals()
code, comparing two null
objects returns true
.
example
Objects.equals()
can be used like this:
String aa = null;
String bb = null;
if (Objects.equals(aa, bb)) {
// ...
}
System.out.println("Objects.equals(null, null): " + Objects.equals(aa, bb));
aa = "google";
bb = "google";
System.out.println("Objects.equals(google, google): " + Objects.equals(aa, bb));
aa= "android";
bb = "google";
System.out.println("Objects.equals(android, google): " + Objects.equals(aa, bb));
result
Objects.equals(null, null): true
Objects.equals(google, google): true
Objects.equals(android, google): false
When comparing two null
objects, Objects.equals
returns true
, so if you don`t like this you should implement something like this:
aa = null;
bb = "google";
if (aa != null && Objects.equals(aa, bb)) {
System.out.println("non null and eqaul");
} else {
System.out.println("null or not eqaul");
}
Alternatively, it can also be implemented using Objects.nonNull()
.
aa = null;
bb = "google";
if (Objects.nonNull(aa) && Objects.equals(aa, bb)) {
System.out.println("non null and eqaul");
} else {
System.out.println("null or not eqaul");
}
Note that Objects also supports nonNull()
and isNull()
methods.
public static boolean isNull(Object obj) {
return obj == null;
}
public static boolean nonNull(Object obj) {
return obj != null;
}
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)