java.lang.NullPointerException

Believe in every java Programmers are no strangers , The communication error of each system is often caused by the lack of a null judgment , abnormal . from java8 start , It provides us with an object null operation processing class Optional.

according to jdk8 Official documents , We can know , The base class exists in java.util Next final class , That is to say, it is a tool class provided by the government .
Of course, first of all, let's look at the specific use methods :
Optional<String> optionalS = Optional.of(new String("baomw"));
System.out.println(optionalS.get());
As above , The code is very simple : The effect is as follows

core api The description is as follows :
method describe

api explain
of Encapsulates the specified value as Optional object , If the specified value is null, Then throw NullPointerException
ofNullable Encapsulates the specified value as Optional object , If the specified value is null, Create an empty Optional object
get If you create Optional Value exists in , Then return this value , Otherwise throw NoSuchElementException
isPresent If you create Optional Value in exists , return true, Otherwise return false
ifPresent If you create Optional Value in exists , Then execute the call to the method , Or nothing
orElse If you create Optional Value exists in , Then return this value , Otherwise, return a default value
orElseGet If you create Optional Value exists in , Then return this value , Otherwise, a Supplier Interface generated values
empty Create an empty Optional object
orElseThrow If you create Optional Value exists in , Then return this value , Otherwise, a specified Supplier Interface generated exception
filter If you create Optional The value in satisfies filter Conditions in , Returns the Optional object , Otherwise, an empty Optional object
map If you create Optional Value in exists , Perform the provided Function function call
flagMap
If you create Optional Value in exists , Perform the provided Function function call , Return to a Optional Value of type , Otherwise, an empty one will be returned Optional object
/** * Common instance for {@code empty()}. */ private static final Optional<?>
EMPTY = new Optional<>(); /** * Returns an {@code Optional} with the specified
present non-null value. * * @param <T> the class of the value * @param value
the value to be present, which must be non-null * @return an {@code Optional}
with the value present * @throws NullPointerException if value is null */
public static <T> Optional<T> of(T value) { return new Optional<>(value); } /**
* Returns an {@code Optional} describing the specified value, if non-null, *
otherwise returns an empty {@code Optional}. * * @param <T> the class of the
value * @param value the possibly-null value to describe * @return an {@code
Optional} with a present value if the specified value * is non-null, otherwise
an empty {@code Optional} */ public static <T> Optional<T> ofNullable(T value)
{ return value == null ? empty() : of(value); } /** * If a value is present in
this {@code Optional}, returns the value, * otherwise throws {@code
NoSuchElementException}. * * @return the non-null value held by this {@code
Optional} * @throws NoSuchElementException if there is no value present * *
@see Optional#isPresent() */ public T get() { if (value == null) { throw new
NoSuchElementException("No value present"); } return value; } /** * Return
{@code true} if there is a value present, otherwise {@code false}. * * @return
{@code true} if there is a value present, otherwise {@code false} */ public
boolean isPresent() { return value != null; }
core api And key points above , Pay attention to comparison of and ofNullable Often used Optional.of establish Optional Null pointer for object times , because ( as follows ) yes of
establish Optional Object construction method , Found that if the value by null Time , A null pointer exception will be thrown .
private Optional(T value) { this.value = Objects.requireNonNull(value); }
public static <T> T requireNonNull(T obj) { if (obj == null) throw new
NullPointerException(); return obj; }
And for ofNullable The above source code can be seen , There is a binocular expression , If value Empty , Create an empty Optional object .
public static <T> Optional<T> ofNullable(T value) { return value == null ?
empty() : of(value); }
As above, the construction method can be selected appropriately according to the actual needs .

It's often written in code like this , Give a default value if the value is empty :
if(str == null){ return new String("baomw"); }else{ return str; }
Of course, if you use Optional.orElse(method) It makes it very simple .
return Optional.ofNullable(str).orElse(new String("baomw"));
use orElse method , That's a good solution , Return if the value is not empty , Otherwise orElse Method inside and return its return value .

About more api Detailed usage of , If you are interested, you can try it yourself .

Technology
©2019-2020 Toolsou All rights reserved,
One and a half years JAVA Summary of work experience Jsp+Ajax+Servlet+Mysql Add, delete, modify and query ( one ) cartoon | CPU Warfare 40 year , The real king finally appeared ! Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood IAR Installation and use tutorial Classical algorithm - recursion ( The case of raw rabbit ) Thorough explanation from Zhongtai Random forest R Language implementation R Language cluster analysis case These songs , Programmers, don't listen !