7

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.

SherlockHomeless
  • 153
  • 2
  • 10
  • 3
    There are some insightful comments here: [java.util.Objects.isNull vs object == null](https://stackoverflow.com/questions/37972859/java-util-objects-isnull-vs-object-null) – d.j.brown Apr 02 '18 at 20:24
  • 3
    You can use them for method reference like Objects::isNull. Equals method is cleaner when using Objects.equals() – Bogdan Lukiyanchuk Apr 02 '18 at 20:25
  • 1
    The docs say "These utilities include null-safe or null-tolerant methods for computing the hash code of an object" so, yes useful if you want generate the hash-code in a null-safe way. – bhspencer Apr 02 '18 at 20:26
  • I think those are mostly intended to be used instead of lambdas with the same functionality, for people who prefer method references. – daniu Apr 02 '18 at 20:26
  • Seems to make some sense, in respect to other Data Structure .java files of the same concept - this is the base class of the DSs, and the implementations are the base functionality for Objects. – Rann Lifshitz Apr 02 '18 at 20:28
  • Yeah as stated you can use the Method reference since Java 8. But I think the bigger benefit and probably the reason they are used so much is it improves readability and maintainability. – Sam Orozco Apr 02 '18 at 20:30
  • 3
    In case of [`Objects.isNull`](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#isNull-java.lang.Object-) (and few others) documentation states: "*API Note: This method exists to be used as a `Predicate`, `filter(Objects::isNull)`*". – Pshemo Apr 02 '18 at 20:43

3 Answers3

16

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • It makes sense for me. Well, is it useful to use them except lambdas? – SherlockHomeless Apr 02 '18 at 20:32
  • 3
    Of the ones you mentioned, only `Objects.equals()`, and possibly `Objects.toString()` (if you're happy with `null` becoming `"null"`). [Check out the others](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html), though; some of them might come in handy. For example, `Objects.requireNonNull()` is nice if you want to protect early against accidental null values and provide a clearer error message. – Aasmund Eldhuset Apr 02 '18 at 20:37
  • 3
    "Objects.toString() and Objects.isNull() (and the more useful Objects.nonNull()) exist for the purpose of being used in lambda expressions." For `Objects.toString()` not really as it was already existed in Java 7 (https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#toString(java.lang.Object)) – davidxxx Apr 02 '18 at 20:53
  • 1
    @Alexander That's not valid method reference syntax. – shmosel Apr 02 '18 at 21:24
  • @shmosel Ahhh it only works for Collectors.toList(), and even then it only works because it's a method (needs () to be called) that returns a lambda D: – Alexander Apr 02 '18 at 21:52
  • It returns a Collector. – shmosel Apr 02 '18 at 21:52
  • @davidxxx: Good point; I edited that in. – Aasmund Eldhuset Apr 02 '18 at 22:36
3

You seem to be asking 3 separate questions, so I'll address them separately:

  1. isNull() and its companion nonNull() were added in Java 8 to be used as method references, similarly to Integer.sum() and Boolean.logicalOr(). For example:

    // Print only non-null elements
    list.stream()
        .filter(Objects::nonNull)
        .forEach(System.out::println);
    
  2. I don't see any advantage in calling Objects.toString() over String.valueOf(). Maybe it was included for uniformity with the other null-safe helpers.

  3. If you know the objects are non-null, go ahead and use Object.equals(). Objects.equals() is meant to be used when they might both be null.

shmosel
  • 49,289
  • 6
  • 73
  • 138
1

In some cases some of these methods don't bring a "great" value and you can suitably ignore them.

1) But as you manipulate classes that miss some checks (check null to prevent NullPointerException) or "optimization" (check first reference equality in equals() for example) , using these Objects methods allow to not be hurt by these and so to keep your client code robust without writing directly all these checks.

2) Another interesting use is for lambda body as you want to use a method reference

3) At last, it allows to make homogeneous the way to perform these very common processings.

These 3 processing rely on 3 different ways :

String.valueOf(o);
if(o == null){...} 
a.equals(b);

While these rely on a single way : utility methods defined in Objects.

Objects.toString(o);
if(Objects.isNull(o)){...}
if(Objects.equals(a, b)){...}
davidxxx
  • 125,838
  • 23
  • 214
  • 215