Autoboxing/Unboxing while casting Integer to int using 'cast' method
问题 Here is a very simple case: I am trying to cast an Object type to a primitive like this: Object object = Integer.valueOf(1234); int result1 = int.class.cast(object); //throws ClassCastException: Cannot convert java.lang.integer to int int result2 = (int)object; //works fine This is the source code of cast method of class 'Class' public T cast(Object obj) { if (obj != null && !isInstance(obj)) throw new ClassCastException(cannotCastMsg(obj)); return (T) obj; } private String cannotCastMsg