Skip to content

Instantly share code, notes, and snippets.

@kui
Created May 25, 2012 09:56
Show Gist options
  • Save kui/2787065 to your computer and use it in GitHub Desktop.
Save kui/2787065 to your computer and use it in GitHub Desktop.
オートボクシング経由すると変換できる。いきなり Integer にするとClassCastException 発生する。
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