Home >Java >javaTutorial >What is the role of java Optional
Explanation
1. Optional is not a function interface, but is to prevent null pointer exceptions.
2. Optional can be regarded as a container that wraps objects (can be null or non-null). As defined as a method, the returned object may be empty or non-null. You can consider using Optional for packaging, which is also the recommended method in Java8.
Example
Optional<String> optional = Optional.of("bam"); optional.isPresent(); // true optional.get(); // "bam" optional.orElse("fallback"); // "bam" optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b"
The basic data types of Java are divided into:
1. Integer type , a data type used to represent integers.
2. Floating point type, a data type used to represent decimals.
3. Character type. The keyword of character type is "char".
4. Boolean type is the basic data type that represents logical values.
The above is the detailed content of What is the role of java Optional. For more information, please follow other related articles on the PHP Chinese website!