(Limited to Java 7 or later) I want you to compare objects in Objects.equals

Comparison before Java 7

Some people (in-house people) still write the following code when comparing objects. To avoid NullPointerException, try writing constants first,

if ("hoge".equals(hoge)) {
	//Never get a NullPointerException

I do null check first and then compare.

if (hoge != null && hoge.equals("hoge")) {
	//If hoge is NULL, the following String.equals is not executed

Comparison after Java 7

If you are using Java 7 or later, please use `java.util.Objects.equals (Object a, Object b)`. In this case, it makes a comparison considering null.

if (Objects.equals(hoge, "hoge")) {

Even if the pointer (address in memory) of the object is different Rest assured that the stored values will be compared with each other.

String hoge1 = "hoge";
String hoge2 = new String("hoge");

System.out.println(hoge1 == hoge2);               // false
System.out.println(hoge1.equals(hoge2));          // true
System.out.println(Objects.equals(hoge1, hoge2)); // true


In writing this article, I revisited java.util.Objects. I didn't know this! I will write something like that.


Method to convert to String type. If the conversion target is null, it will be converted to the character string null.

Object obj = null;
String str = Objects.toString(obj);
System.out.println(Objects.equals(str, "null")); // true
System.out.println(Objects.equals(str, null));   // false 

It's very nice to be able to avoid nulls, but I think there are some situations where it's difficult to convert to the string null. In such a case, you can change the value returned when null by setting a character string in the second argument.

Object obj = null;
String str = Objects.toString(obj, "It's NULL!");
System.out.println(str); //Output result: NULL!

I knew the toString method, but I didn't know that I could set the second argument.


isNull is a method that returns true if the argument is NULL. nonNull is a method that returns false if the argument is NULL.

Object obj = null;
System.out.println(Objects.isNull(obj));  // true
System.out.println(Objects.nonNull(obj)); // false

obj == nullLooks more stylish. (I didn't see the difference in behavior between obj == null and obj! = null ...)


A method to check if the target is NULL. If it is NULL, a NullPointerException will occur.

Object obj2 = Objects.requireNonNull(obj1);

If you try to do a null check without using this method

if (obj1 == null) {
	throw new NullPointerException();
} else {
	Object obj2 = Objects.requireNonNull(obj1);

Will be. I think it's a simple and convenient method.


Objects (Java Platform SE 7) The old way of writing is old! ?? java.util.Objects is too convenient

Recommended Posts

(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I want to send an email in Java.
rsync4j --I want to touch rsync in Java.
I want to do something like "cls" in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
When you want to dynamically replace Annotation in Java8
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to simplify the conditional if-else statement in Java
[CQ Engine] I want to handle collections like Stream or .Net LINQ even in Java 7.
[Java] I want to perform distinct with the key in the object
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
How to remote debug Java 9 or later
I want to stop Java updates altogether
I want to use @Autowired in Servlet
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
[Java Spring MVC] I want to use DI in my own class
Run R from Java I want to run rJava
I want to use arrow notation in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
When you want to bind InputStream in JDBI3
I tried to implement deep learning in Java
I want to use java8 forEach with index
I want to pass APP_HOME to logback in Gradle
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want you to use Scala as Better Java for the time being
I tried to output multiplication table in Java
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I tried to create Alexa skill in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to get the value in Ruby
You also need to specify the host when debugging remotely with Java 9 or later
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
I want to return an object in CSV format with multi-line header & filter in Java
# 1_JAVA I want to get the index number by specifying one character in the character string.
Code to use when you want to process Json with only standard library in Java
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to embed any TraceId in the log
[Gradle] Generate Javadoc including JavaScript in Java 1.8.0_121 or later
I tried to implement Firebase push notification in Java
I want to use fish shell in Laradock too! !!
# 2 [Note] I tried to calculate multiplication tables in Java.
I want to use a little icon in Rails
I tried to create a Clova skill in Java
I tried to make a login function in Java
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
How to convert A to a and a to A using AND and OR in Java
I want to stop snake case in table definition
If you want to recreate the instance in cloud9
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I want to get along with Map [Java beginner]
I tried to find out what changed in Java 9