Created
May 25, 2012 09:56
-
-
Save kui/2787065 to your computer and use it in GitHub Desktop.
オートボクシング経由すると変換できる。いきなり Integer にするとClassCastException 発生する。
This file contains hidden or 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.HashMap; | |
import java.util.Map; | |
public class App | |
{ | |
public static void main( String[] args ) throws Exception | |
{ | |
Map<?, ?> m = new HashMap<Object, Object>(); | |
((HashMap<String, Long>) m).put("foo", 1L); | |
Integer i = (int) ((long) ((HashMap<String, Long>) m).get("foo")); // => OK | |
System.out.println(i); | |
Integer j = ((HashMap<String, Integer>) m).get("foo"); // => ClassCastException | |
System.out.println(j); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment