| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This is the tutorial on java.util.Optional<T>
You're supposed to be familiar with OOP, have basic knowledge of JDK, and be able to write Java code.
When writing a method that is not able to return a value in some cases, there are three options:
Returning null is obliously the most error prone approach. If you want to be safe, you should somehow force clients of your method to handle the possible lack of value. For that purpose you can use checked exceptoins. Although this approach is safe it has two disatvantages: thowing exceptoin is an expansive operatoin, and it brings addtional boilerplate. The better approach is to use Optional<T>, which is safe and concise.
Optional<T> is a like a collection that can hold at most one value. It provides usefull methods to replace imperative old-style null-checks. It also contains convenient methods for working with Stream API. In general, Optional API follows the similar functional-style approach as streams.
| Back | FazBrowse Home | New Git URL |