Is there any benefit to use methods of Objects.java?

SherlockHomeless :

I examined methods of Objects.java, but i couldn't find too much useful sides of that methods. For Example the code that will work when i use Objects.isNull :

public static boolean isNull(Object obj) {
    return obj == null;
}

There are the two ways for checking nullity of two objects :

if(o == null)
if(Objects.isNull(o))

So there are not so many differences between them. Another example the code that will work i use Objects.toString

public static String toString(Object o) {
    return String.valueOf(o);
}

When i use it It calls toString of object at background.(With only one difference it writes "null", if the object is null because it uses String.valueOf()

And Objects.equals :

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

It will makes null check in every check(without knowing it is necessary or not.)

Am i wrong? If i am, why should i use that methods and other methods of Objects.java?

EDIT

I did not asked this question only for Objects.isNull and Objects.nonNull, i want to know purpose, usability(except for lambdas also) and benefits of Objects class and its methods. But in javadoc is written that only for Objects.isNull and Objects.nonNull have purpose to use with lambdas(as predicate filter(Objects::isNull)). I want to know others as well.

Aasmund Eldhuset :

Objects.isNull(), and the more useful Objects.nonNull(), exist for the purpose of being used in lambda expressions. Objects.toString() was introduced for null safety (as pointed out by @davidxxx), but is also very useful in lambdas.

For example, list.stream().filter(Objects::nonNull).map(Objects::toString) will give you a Stream<String> with the results of calling toString() on all the elements in list that are not null.

Objects.equals() is useful precisely when you know that the objects you're comparing might be null, as it saves you some typing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=458759&siteId=1