我有以下代码:
static Object f(Object x) {
x = (Integer) 1234; // <- it runs OK (why?)
System.out.println(x);
return x;
}
public static void main(String[] args) {
HashMap<String, String> before = new HashMap<String, String>();
before.put("a", "b");
HashMap<String, String> after = (HashMap<String,String>) f(before); // <- it fails
System.out.println(after);
}
这是我的输出:
1234
Exception in thread "main" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.util.HashMap
为什么从 HashMap 到 Intger 的转换运行没有错误?