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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an AI-Powered Code Review Assistant in Java
  • Modernizing a Legacy Java Application With OpenRewrite: What We Automated and What We Didn't
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade

Trending

  • DZone's Article Submission Guidelines
  • When to Use AMQP, JMS, Kafka, or MQTT: Trade-Offs, Not a Winner
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Kafka vs Flink vs Spark: Do You Really Need Real-Time?

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Jul. 31, 22 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Building an AI-Powered Code Review Assistant in Java
  • Modernizing a Legacy Java Application With OpenRewrite: What We Automated and What We Didn't
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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