In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to avoid null pointer exceptions in Java8". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to avoid null pointer exceptions in Java8.
Preface
When it comes to which exception you are most impressed with in Java programming, the NullPointerException null pointer is notorious. Not to mention that junior programmers will encounter, even intermediate, expert programmers will fall into this hole without paying attention.
Tony Hoare, the inventor cited by Null, issued an apology in 2009, saying that so far, null pointer anomalies have caused billions of dollars in losses to companies.
Here are the exact words of Tony Hoare:
I call the design cited by Null a multibillion-dollar mistake. In 1965, I was designing the first fully functional system in object-oriented language (ALGOL W). My consideration at the time was to make sure that all references used were safe and that the compiler would check them automatically. However, I didn't resist the temptation to add Null references, just to make it easy to implement. Since then, it has led to countless bug, errors and system crashes, as well as incalculable losses for enterprises.
It has come to this, and we must learn to face it. So, how can we prevent null pointer exceptions?
The only way is to add checks on objects that might be Null. But Null checking is tedious and painful. So some newer languages specifically add special syntax, such as the null merge operator, to handle Null checking.
It is also called the Elvis operator in languages such as Groovy or Kotlin.
Unfortunately, such syntactic sugars were not provided in older versions of Java. Improvements have been made in this area in Java8. Therefore, this article is intended to introduce how to use new features in Java8 to prevent the occurrence of NullPointerException.
How to strengthen the inspection of Null objects in Java8?
In the previous article, Java8's new features guide briefly mentioned how to do a null check on an object through the Optional class. Next, let's talk about it in more detail:
In a business system, nesting objects in objects is a common scenario, as shown in the following sample code:
/ / outermost object class Outer {Nested nested; Nested getNested () {return nested;}} / / second layer object class Nested {Inner inner; Inner getInner () {return inner;}} / / lowest level object class Inner {String foo; String getFoo () {return foo;}}
In business, suppose we need to get the foo property in the underlying Inner of the Outer object, we have to write a bunch of non-empty checks to prevent NullPointerException from happening:
/ / tedious code Outer outer = new Outer (); if (outer! = null & & outer.nested! = null & & outer.nested.inner! = null) {System.out.println (outer.nested.inner.foo);}
Through Optional
In Java8, we have a more elegant solution, which is to use Optional, which means that we can do pipelined map operations in one line of code. The null check is automatically performed inside the map method:
Optional.of (new Outer ()) .map (Outer::getNested) .map (Nested::getInner) .map (Inner::getFoo .ifPresent (System.out::println); / / if it is not empty, the final output value of foo
Enhance API by customizing the suppiler function
I still feel a little verbose in this way. We can use the suppiler function to come up with an ultimate solution:
Public static Optional resolve (Supplier resolver) {try {T result = resolver.get (); return Optional.ofNullable (result);} catch (NullPointerException e) {/ / may throw a null pointer exception and directly return an empty Optional object return Optional.empty ();}}
Use the above resolve method to reconstruct the above non-empty parity code segment:
Outer obj = new Outer (); / / call the resolve method directly, and internally short the pointer resolve (()-> obj.getNested (). GetInner (). GetFoo ()); .ifPresent (System.out::println); / / if it is not null, finally output the value of foo
At this point, I believe you have a deeper understanding of "how to avoid null pointer exceptions in Java8". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.