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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Otavio Santana user avatar by
Otavio Santana
CORE ·
Jul. 31, 22 · Presentation
Like (5)
Save
Tweet
Share
13.62K 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.

Popular on DZone

  • Implementing Adaptive Concurrency Limits
  • Apache Kafka vs. Memphis.dev
  • What Was the Question Again, ChatGPT?
  • Comparing Kubernetes Gateway and Ingress APIs

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: