Created
March 30, 2011 17:12
-
-
Save bxt/894819 to your computer and use it in GitHub Desktop.
Casting null to Date in Java, avoiding ambiguous method calls
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
import java.util.Date; | |
public class Nullcast { | |
private static void foo(String bar) { | |
System.out.println("Not here...\n"); | |
} | |
private static void foo(Date bar) { | |
System.out.println("Indeed here, even"); | |
System.out.println((bar instanceof Date?"with Date":"without Date!")); | |
} | |
public static void main(String[] args) { | |
foo((Date)null); | |
} | |
} | |
/* | |
Wow you can cast null to Date and: it won't be a Date... | |
I wonder how this looks in bytecode. Output: | |
$ javac -g Nullcast.java | |
$ java -cp . Nullcast | |
Indeed here, even | |
without Date! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment