Java 9 introduce new static java.util.Objects.requireNonNullElse method in Java.util.Objects which is very useful when object assign or pass argument with default object.

java.util.Objects.requireNonNullElse require two parameters while perform assignment operation, If first object is null than its will assign or return second parameter.

Example:

Employee employeeNull=null;
Employee defaultEmployee=new Employee();
defaultEmployee.setName("Default Employee");
Employee newEmployee=java.util.Objects.requireNonNullElse(employeeNull,defaultEmployee);
System.out.println(newEmployee.getName());

 Output:

Default Employee

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *