Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
12.0, 11.2, 11.3, 12.2
-
None
-
OpenJDK 11.0.2
Gradle 6.4.1
Description
public static void main(String[] args)
{ Optional.ofNullable(getValue())//as instance of java.util.Optional .map((o)->o instanceof String ? ((String)o).substring(0) : o.toString()); }private static Object getValue()
{ return ""; }o.toString() show hint about dereferencing possible null pointer, but this is not possible in that case as check is ensured by optional. In example is used "String" for simplification, but in more abstract case you want to get value one way if it is instance of anything and other way from "o" if it is not instance of that type.