Sub topic 8
How null behaves
This chapter assumes you know basic Java syntax, reference types, inheritance, the instanceof operator, casts, Boolean operators, and the structure of an equals method.
A type pattern does not change the null rule of instanceof. The expression value instanceof Type is false when value is null. The pattern variable is therefore not initialized for null.
Null never matches an instanceof pattern
Consider this complete example:
public class NullPatternDemo {
static String describe(Object value) {
if (value instanceof String text) {
return "String of length " + text.length();
}
return "Not a String";
}
public static void main(String[] args) {
Object value = null;
System.out.println(value instanceof String);
System.out.println(value instanceof String text);
System.out.println(describe(value));
}
}The program prints:
false
false
Not a StringThe second test does not throw a NullPointerException. Java first checks whether value refers to a non-null object of the requested type. Because null refers to no object, the pattern does not match.
The binding variable text is available only when the pattern matches. For null, the if body is skipped, so text cannot be used there.
Null compared with an explicit cast
A cast has a different immediate result:
Object value = null;
String text = (String) value;This cast succeeds and assigns null to text. The cast does not prove that text refers to a usable String. Calling text.length() afterward throws a NullPointerException.
A pattern combines the type test and the safe entry into the body:
if (value instanceof String text) {
System.out.println(text.length());
}The body runs only for a non-null String. This is useful when the operation inside the body requires a real object.
| Value | Pattern result | Is the binding variable available in the matching body? |
|---|---|---|
| null | false | No |
| a String object | true | Yes |
| an Integer object | false | No |
Null and compound conditions
With &&, the right side runs only when the left side is true. Therefore, a binding variable can be used on the right side:
static boolean isNonEmptyString(Object value) {
return value instanceof String text && !text.isEmpty();
}For null, the first condition is false. Java short-circuits the expression, so it does not evaluate text.isEmpty(). The method returns false without an exception.
Do not replace && with || when the right side needs the binding variable:
// Does not compile:
// return value instanceof String text || text.isEmpty();If the value is null, the left side is false, so the right side would need to run. But no successful pattern match established text. The compiler rejects this use.
Practical rule
Treat a type pattern as both a non-null check and a type check. If you need to distinguish null from another nonmatching value, test null separately:
if (value == null) {
return "missing";
}
if (value instanceof String text) {
return text;
}
return "other type";Exercise
What does this method return when called with null, and why? static boolean valid(Object value) { return value instanceof String text && text.length() > 0; }
Show answer
It returns false. The instanceof pattern is false for null, so && short-circuits and the length check is not evaluated.