Created
October 11, 2011 00:01
-
-
Save chrisyco/1276918 to your computer and use it in GitHub Desktop.
Autoboxing is not foolproof
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
// 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