Take the ʻIntegerList` class as an example to get a list and elements of Integers containing nulls.
IntegerList.java
import java.util.List;
import java.util.Arrays;
public class IntegerList {
//List of Integers containing null
private final static List<Integer> INTEGER_LIST = Arrays.asList(new Integer[]{1, 2, null});
public static Integer get(int index) {
return INTEGER_LIST.get(index);
}
}
Prepare a Main
class that gets a numeric element from the above ʻIntegerList, adds 100, and outputs it. Considering the possibility that the return value of ʻIntegerList # get
is null, set -1 if it is null.
Main.java
/**
*Get the value from IntegerList, add 100 and output
**/
public class Main {
public static void main(String[] args) throws Exception {
Integer value = IntegerList.get(2);
//If null-Set to 1
if (value == null) {
value = -1;
}
value += 100;
System.out.println("result:" + value);
}
}
Result: 99
You can write it as follows:
Main.java
import java.util.Optional;
/**
*Get the value from IntegerList and add 100
**/
public class Main {
public static void main(String[] args) throws Exception {
//Get the element from the IntegerList class. If null-Set to 1.
Integer value = Optional.ofNullable(IntegerList.get(2)).orElse(-1);
value += 100;
System.out.println("result:" + value);
}
}
The if statement disappeared and I could write it in one line.
The code is clean! Optional Convenient! !! It seems that we shouldn't end with the story.
If you want to loosely couple relationships between classes
It seems strange that the calling Main
class makes the return value of ʻIntegerList # get Optional. I was able to write the code to make it Optional because I knew that the return value of ʻIntegerList # get
could be null.
What you know is that the relationship is more or less close.
And, conversely, if you do not notice the possibility of becoming null, neither Optionalization nor null check will be written, and you will allow the occurrence of NullPointerException
.
Instead of the caller making the return value Optional, the callee returns Optional as the return value.
IntegerList.java
import java.util.List;
import java.util.Arrays;
import java.util.Optional;
public class IntegerList {
//An array of Integers containing null
private final static List<Integer> INTEGER_LIST = Arrays.asList(new Integer[]{1, 2, null});
//Since the return value may contain null, wrap it in Optional and return it.
public static Optional<Integer> get(int index) {
return Optional.ofNullable(INTEGER_LIST.get(index));
}
}
Main.java
import java.util.Optional;
/**
*Get the value from IntegerList and add 100
**/
public class Main {
public static void main(String[] args) throws Exception {
//Get the element from the IntegerList class. If null-Set to 1.
Optional<Integer> optionalValue = IntegerList.get(2);
Integer value = optionalValue.orElse(-1) + 100;
System.out.println("result:" + value);
}
}
By setting Optional as the return value, it is possible to clarify that the return value of ** ʻIntegerList # getmay be null, and to force the caller to code considering null. ** ** This will prevent the caller from forgetting to write the null verdict and getting a
NullPointerException`.
If the above doesn't work, you can refer to the following.
Summary of stories and usage of Optional not Serializable
Therefore, the usage of Optional is basically only as the return value of the method, and it is better not to use it for the next field.
Use Optional type instead of null in Java8
The advantage is that wrapping the value in this prohibits direct tampering, thereby syntactically forcing the program to build on the premise that "the value may not be returned". In short, it's the syntax (class) for forcing null checking.
re: Maybe / Nullable / Optional doesn't really fit me.
Java Optional seems to be supposed to be used as the return value of the method, not to be used for arguments or fields. Given that Java's Optional is a reference type, it can be null, so it's understandable to some extent that it shouldn't be used as an argument.
Java often has a method that "returns a value, but cannot return a value only in certain cases". 例えばjava.util.Map#get()は、キーがあればそれに紐付く値を返すが、キーが無い場合はnullを返す。 For example, String # indexOf () returns the position of the found character, but returns -1 if not found. The role of Optional is to express the return value of such a method.
The advantage of using Optional is that you can make it clear in your code that its value may not be there. In the above example, the acquired left and right are optional, so you will be forced to describe the processing in the case of empty without being careful.
10 Tips To Handle Null Effectively
As I already said, Optional was designed to indicate missing return values. One tempting case that it was not designed for and surely is not necessary is class’ fields. Although it is in English, it was helpful because it was packed with thoughts on coding for null.
Recommended Posts