DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • AI Paradigm Shift: Analytics Without SQL
  1. DZone
  2. Coding
  3. Java
  4. Optional in Java: When Better Code Is Not an Alternative

Optional in Java: When Better Code Is Not an Alternative

Learn how to take advantage of this API and combine it to create a better, maintainable code design for your Java projects.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Jul. 31, 22 · Presentation
Likes (5)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

If you are a Java developer, you have handled issues with Java for a long time. The most often problem in the Java world is the NullPointerException.

Nullity is a natural consequence of reference types. If there is a reference, it has to refer to some object - or be null. If there is no this option, all the variable was initialized with some non-null expression - and even then, you'd have issues if variables were read during the initialization phase.

Once this issue happened several times and brought bugs, this issue became the Billion Dollar Mistake.

When we avoid this mistake, we have several patterns, such as Null values: In object-oriented computer programming, a null object is an object with no referenced value or defined neutral ("null") behavior. The null object design pattern describes such objects uses and behavior (or lack thereof).

Even with the Null values pattern, for some cases, we need to check if the instance is null, and if it is not null, we can do some action on it.

Java
 
Person otavio = database.findByName("Otavio);

if(otavio != null) {
  //do some action here
}

The exception to these rules is the collections where we always should return references such as an emptyList using the principles from the effective Java.

Since Java 8, Java Developers have one good way to avoid the classic NullPointerException. However, it is still a problem to use it optimally. This video will help you combine this API with your business. It includes an enrichment to apply your entities with your rich model.

We can see the use of this API in several frameworks, such as Spring Framework, which has Spring Data where it returns an Optional reference in the repository interfaces.

The goal is to return an Optional in each value that might be null such as the find by id method. This way, a user can operate with this alternative and check this prospect inside the Java API.

This video will explore the API available in the Java world since Java 8 from a regular programmer's perspective and how we can use and explore this API more inside our code, not as a framework.

Are you ready to understand the pitfalls of this API and how to archive a clean and safe design in your code? So this video is for you. 

We'll understand:

  • The performance impact both: memory and process.
  • Why premature performance is dangers
  • We'll explain how to apply Optional with our entities to make creating and maintaining a rich model easier.
  • We have several data frameworks, such as Spring Data, but how to combine this with our current service with controller layers?
  • As with any trade-off, we also need to understand why we should not use the Optional API.
  • Much more!

Thus, after this video, we'll learn the best way to apply the Optional API, how to combine it with old and new APIs, and mainly, to take advantage of this new API to avoid inconsistency such as the billion-dollar mistake: Null pointer exception.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook