Studying Java 8 (Optional)

Optional study sample

/**
 *Optional study
 * @author komikcomik
 *
 */
public class OptionalHello {
	public static void main(String[] args) {
String null= null;
String is not null= "abc";

		if (Optional.ofNullable(null).equals(Optional.empty())) {
			System.out.println("Was null");
		} else {
			System.out.println("It wasn't null");
		}

		if (Optional.ofNullable(It's not null).equals(Optional.empty())) {
			System.out.println("It was null 2");
		} else {
			System.out.println("It wasn't null 2");
		}

		Optional.ofNullable(null).ifPresent(s -> System.out.println(s));
		Optional.ofNullable(It's not null).ifPresent(s -> System.out.println(s));
	}

}

Execution result

Was null
It wasn't null 2
abc

The example is not cool this time, but it seems that it is actually good to use Optional for the return value of the method.

Recommended Posts

Studying Java 8 (Optional)
Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying java9 (jShell)
Studying Java 8 (Stream)
[Java] Optional memorandum
Java 9 Optional :: stream
Studying Java 8 (Collector / Collectors)
View Java Optional Javadoc
Studying Java 8 (see constructor)
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Rewrite Java try-catch with Optional
How to use java Optional
[Java] How to use Optional ②
Java
Java8 to start now ~ Optional ~
Java
[Java] How to use Optional ①
Compare Java 8 Optional with Swift
Basic usage of java Optional Part 1
Studying Java 8 (StaticIF and Default methods)
Studying Java 8 (String Joiner and join)
Studying java9 (dropWhile, takeWhile and Stream.ofNullable)
Java learning (0)
[Java] array
Conversion between Kotlin nullable and Java Optional
Java protected
[Java] Annotation
[Java] Module
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
Studying Java 8 (date API in java.time package)
JAVA paid
Java (set)
java shellsort
[Java] compareTo
java reflexes