Skip to content

Instantly share code, notes, and snippets.

@philz
Created August 13, 2013 22:24
Show Gist options
  • Save philz/6226305 to your computer and use it in GitHub Desktop.
Save philz/6226305 to your computer and use it in GitHub Desktop.
Ternary Boxing -- Java Puzzler -- What does this print?
// License: Public Domain
//
// Java Puzzler: what does this print?
public class TernaryBoxing {
public static void main(String[] args) {
Long nullLong = null;
try {
takesLong(true ? nullLong : 1);
} catch (NullPointerException e) {
System.out.println("1");
}
try {
takesLong(true ? nullLong : 1L);
} catch (NullPointerException e) {
System.out.println("2");
}
try {
takesLong(true ? nullLong : (Long) 1L);
} catch (NullPointerException e) {
System.out.println("3");
}
}
public static void takesLong(Long x) {
System.out.println(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment