Skip to content

Instantly share code, notes, and snippets.

@bxt
Created March 30, 2011 17:12
Show Gist options
  • Save bxt/894819 to your computer and use it in GitHub Desktop.
Save bxt/894819 to your computer and use it in GitHub Desktop.
Casting null to Date in Java, avoiding ambiguous method calls
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