Is there any use for using Objects.java methods?

I studied the Objects.java methods, but I could not find too many useful aspects of these methods. For example, the code that will work when I use Objects.isNull:

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

There are two ways to check the invalidity of two objects:

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

Thus, there are not many differences between them. Another example of code that will work, I use Objects.toString

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

When I use it, it calls the object's toString in the 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 check for zero in each check (not knowing whether it is necessary or not.)

Am I mistaken? If I, why should I use these methods and other methods of the Object.java object?

EDIT

I did not ask this question only for Objects.isNull and Objects.nonNull, I want to know the purpose, usability (except lambdas) and the advantages of the Objects class and its methods. But in javadoc it is written that only for Objects.isNull and Objects.nonNull there is a goal to use with lambdas (as a filter of predicates (Objects :: isNull)). I want to know others .

+5
source share
3 answers

Objects.isNull() , and the more useful Objects.nonNull() , exist for the purpose of use in lambda expressions. Objects.toString() was introduced for zero security (as @davidxxx pointed out), but is also very useful in lambdas.

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

Objects.equals() is useful just when you know that the objects you are comparing can be NULL, as this will save you some typing.

+11
source

It seems you are asking 3 separate questions, so I will consider them separately:

  • isNull() and its companion nonNull() were added in Java 8, which will be used as references to methods, similar to Integer.sum() and Boolean.logicalOr() . For instance:

     // Print only non-null elements list.stream() .filter(Objects::nonNull) .forEach(System.out::println); 
  • I see no benefit when calling Objects.toString() over String.valueOf() . It may have been included for consistency with other null safe assistants.

  • If you know that objects are not null, continue to use Object.equals() . Objects.equals() implies that they can be zero.

+3
source

In some cases, some of these methods do not bring a "big" value, and you can ignore them.

1) But since you are manipulating classes that skip some checks (check null to prevent a NullPointerException ) or โ€œoptimizationโ€ (check, for example, the first reference equality in equals() ) using these Objects methods do not suffer from these and keep the code reliable customer without directly recording all of these checks.

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

3) Finally, this allows a uniform way of performing these very general processes.

These 3 treatments rely on 3 different methods:

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

Although they rely on one way: utility methods defined in Objects .

 Objects.toString(o); if(Objects.isNull(o)){...} if(Objects.equals(a, b)){...} 
+1
source

Source: https://habr.com/ru/post/1276218/


All Articles