What is optional and why it is there? | Java8
Have you ever been tired of null
NullPointerExceptions
. Java 8 has introduced a new class Optional
in java.util
package. It is used to represent a value is present or absent. The main advantage of this new construct is that No more too many null checks NullPointerException
NullPointerExceptions
- Optional: Null checks are not required.
- No more Boilerplate code
- No more NullPointerException at run-time.
- Clean and neat APIs can be developed
The catch with this new Optional class is, of course, the word "class". Optional is only a wrapper that contains a reference to some other object and isn't close to being a panacea for NullPointerExceptions.
Let's see, an example of how we can create and use optional:
There are many ways to create
To create optional with non-null values you can write as below:
Employee employee = new Employee(); Optional<Employee> op = Optional.of(employee);
To create optional with an empty value you can write as below:
Optional<Employee> employee = Optional.empty();
Now, let's make use of the optional to NullPointerException
With out Optional:
if (project != null) { ApplicationType applicationType = project.getApplicationType(); if (applicationType != null) { String typeDirName = applicationType.getTypeDirName(); if (typeDirName != null) { System.out.println(typeDirName); } } }
now with optional we can re-write above code using optional and streams 😉
Optional<String> optionalTypeDirName = optionalProject .flatMap(project -> project.getApplicationTypeOptional()) .flatMap(applicationType -> applicationType.getTypeDirNameOptional()); optionalTypeDirName.ifPresent(typeDirName -> System.out.println(typeDirName));
Even I can see some of the disadvantages of using Optional:
- Not serializable making it useless for many cases.
- Optional is a wrapper which means if you use it you'll now have two object references where you used to have one.
- In Some cases, it makes the debugging even worse nightmare.
That is all for Optional now. To explore some more java8 feature you can check this article: Java8 features.
Some of the good to know features are: Lambda Expressions, Functional Interfaces
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Java, Java8, Java8 Features, optional
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.