Skip to content

Instantly share code, notes, and snippets.

@chrisyco
Created October 11, 2011 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisyco/1276918 to your computer and use it in GitHub Desktop.
Save chrisyco/1276918 to your computer and use it in GitHub Desktop.
Autoboxing is not foolproof
// Duck.java
class Duck {
public static void quack(Integer num) {
System.out.println(num);
}
}
// Main.java
class Main {
public static void main(String[] args) {
Duck.quack(42);
}
}
/*
How to make this program crash
==============================
Compile both classes:
$ javac Duck.java
$ javac Main.java
Execute main:
$ java Main
It should say 42. Autoboxing works.
-----
Now change the line
.. quack(Integer num) {
to
.. quack(int num) {
Re-compile Duck, but *don't* re-compile Main:
$ javac Duck.java
Now try running Main:
$ java Main
Exception in thread "main" java.lang.NoSuchMethodError: Duck.quack(Ljava/lang/Integer;)V
at Main.main(Main.java:5)
Crash!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment