Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java 8 - Optionals

164 views

Published on

Java 8 provides a new way to deal with null references called Optionals. In these slides I cover why null was called the "billion dollar mistake" and how to mitigate it's damage.

Published in: Software
  • Be the first to comment

Java 8 - Optionals

  1. 1. Java 8 Optionals are not optionals
  2. 2. What’s wrong with null?
  3. 3. What’s wrong with null? • Null references were introduced in 1965 byTony Hoare while designingALGOLW. • He thought it was the most convenient way to model the absense of a value. • Many programming languages followed this approach. • After many years he sees his decisión as “my billion-dollar mistake”.
  4. 4. What’s wrong with null? A little example
  5. 5. What’s wrong with null? A little example
  6. 6. What’s wrong with null? A little example
  7. 7. What’s wrong with null?
  8. 8. What’s wrong with null? • NullPointerException is one of the most common exceptions thrown in Java. • It worsens readability. • It’s the only thing in Java that makes pointers are not hidden.
  9. 9. Optional to the rescue
  10. 10. Optional to the rescue What is an Optional? • A class that encapsulates an optional value. • When the value is present, the Optional just wraps it. • If the value is not present, the OptionalWill be a special singleton instance representing an empty Optional.
  11. 11. Optionals to the rescue A little example
  12. 12. Optionals to the rescue A little example
  13. 13. Optionals to the rescue A little example
  14. 14. Optionals to the rescue A little example
  15. 15. Optionals to the rescue A little example
  16. 16. Creating Optionals
  17. 17. Creating Optionals Empty Optional
  18. 18. Creating Optionals An Optional from a non-null value
  19. 19. Creating Optionals Optional from null
  20. 20. Transforming Optionals
  21. 21. Transforming Optionals Extracting information
  22. 22. Transforming Optionals Chaining Optionals
  23. 23. Transforming Optionals Unwrapping Optionals
  24. 24. Transforming Optionals Rejecting Optional values
  25. 25. The End

×