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.


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


Default Employee


Was this post helpful?

Tags: ,

Leave a Reply

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