Created
August 13, 2013 22:24
-
-
Save philz/6226305 to your computer and use it in GitHub Desktop.
Ternary Boxing -- Java Puzzler -- What does this print?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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