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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how Java 8 Optional elegantly avoids NPE. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Null pointer exception (NullPointException, referred to as NPE) can be said to be an exception encountered by all Java programmers. Although Java tries to get programmers out of the pain of pointers from the beginning of their design, pointers do exist, and Java designers can only make pointers easier to use in the Java language, rather than completely eliminate them, so they have the common keyword null.
Avoid using null checking
A null pointer exception is a run-time exception, and if there is no clear way to handle it, the best practice is to let the program die early. When the exception does occur, the handling is also very simple, just add an if statement to determine where the exception exists. For example, the following code:
Public String bindUserToRole (User user) {if (user = = null) {return;} String roleId = user.getRoleId (); if (roleId = = null) {return;} Role = roleDao.findOne (roleId); if (role! = null) {role.setUserId (user.getUserId ()); roleDao.save (role);}}
However, such a response will lead to more and more null decisions in the program. A good program design should minimize the occurrence of the null keyword in the code, so Java 8 introduces the Optional class to avoid NPE problems and improve the aesthetics of the code. However, it is not an alternative to the null keyword, but provides a more elegant implementation for null determination, thus avoiding NPE problems.
Optional class
In order to better solve and avoid common NPE problems, Java 8 introduces a new class java.util.Optional,Optional value that can be null. If the value exists, call the isPresent () method to return true, and call the get () method to get the value.
Create an Optional object
The Optional class provides three class methods for instantiating an Optional object, which are empty (), of (), and ofNullable (), all static methods that can be called directly.
The empty () method is used to create an Optional object with no value:
Optional emptyOpt = Optional.empty ()
The object created by the empty () method has no value. Calling the isPresent () method on the emptyOpt variable returns false, and calling the get () method throws a NPE exception.
The of () method creates an Optional object with a non-empty value:
String str = "Hello World"; Optional notNullOpt = Optional.of (str)
The ofNullable () method receives a value that can be null:
Optional nullableOpt = Optional.ofNullable (str)
If the value of str is null, the resulting nullableOpt is an Optional object with no value.
Get the value in the Optional object
If we want to get the roleId attribute value in the User object, the common way is to get it directly:
String roleId = null;if (user! = null) {roleId = user.getRoleId ();}
Using the map () method provided in Optional makes it easier to do this:
Optional userOpt = Optional.ofNullable (user); Optional roleIdOpt = userOpt.map (User::getRoleId)
Use the orElse () method to get the value
The Optional class also contains other methods for getting values, which are:
OrElse (): returns if there is a value, otherwise returns a given value as the default value
OrElseGet (): works similarly to the orElse () method, except that default values are generated in different ways. This method accepts a Supplier
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.