The program is run with java -ea Trial81 - what will be the output?
public class Trial81
{
interface Greeter
{
default String greet()
{
assert name() != null : "name was null";
return "hi " + name();
}
String name();
}
public static void main(String[] args)
{
Greeter g = () -> null;
System.out.println(g.greet());
}
}
Answer: A
Assertions are legal inside a default method, and the lambda supplies a name implementation returning null. The assertion in the default method therefore fails and throws with its message. Without -ea the method would go on to print hi null.
Please log in or register to report this question.