Java 8 – general practice using Optional

0. Never, ever use null for an Optional variable.
1. Optional should be only used as a return value.
: never use it for a field in Class and a method parameter.
never use it in collections

2. Never try to call .get() method on an Optional.
: if it is a situation to get a value, ifPresent, map and orElse() family can solve this problem .

3. Remember Optional is not Serializable.

Note)
Java 9 will introduce the following new method

Optional.stream()
Optional.ifPresentOrElse()
Optional.or()
Advertisements
This entry was posted in Java, Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s