Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
Integer x = null;
System.out.println(x);
int y = x;
System.out.println(y);
}
}Prints null, then a NullPointerException is thrown
- Expecting
0afternull: unboxing is a hiddenx.intValue()call, so anullwrapper throwsNullPointerExceptioninstead of turning into zero. - Expecting the error at
println(x): printing anullreference is fine and printsnull; only the unboxing line fails.
- What is the default value of a
static Integerfield? - How do you get an
intsafely from anIntegerthat may benull?