Java optional class usage sharing
Optional is defined as a container class in Java, more specifically, a container with only one element.
container object which may or may not contain a non-null value.
Optional class structure optional property
The optional class contains two properties:
Class attribute: empty object attribute: value
The empty property is used to store an optional object with null value. The value attribute is used to store non null objects.
Optional method
Optional has two construction methods, both of which are modified by private.
The parameterless constructor is used to initialize empty. The parameterized constructor is used to initialize non null objects.
Because the constructor is decorated as private, optional wants to instantiate objects only through class methods. Optional provides three class methods.
Empty: returns an optional object with null value of: returns an optional object with non null value ofnullable: the value of value returns the corresponding optional object according to whether the parameter is null
Introduction of example method