June 2018
Intermediate to advanced
310 pages
6h 32m
English
Probably the most notorious exception in the Java world is NullPointerException.
The reason behind this exception is that every object in Java can be null. The code here shows us why:
String s = "Hello";...s = null;System.out.println(s.length); // Causes NullPointerException
In this case, marking s as final would prevent the exception.
But what about this one:
public class Printer { public static void printLength(final String s) { System.out.println(s.length); }}
From anywhere in the code it's still possible to pass null:
Printer.printLength(null); // Again, NullPointerException
Since Java 8, there's been an optional construct:
if (optional.isPresent()) { System.out.println(optional.get());}
In a more functional style:
optional.ifPresent(System. ...
Read now
Unlock full access