黑马程序员技术交流社区

标题: 从反射的方法调用看java存在运行时“拆箱”行为。你们怎么看? [打印本页]

作者: justin1258    时间: 2014-8-6 22:29
标题: 从反射的方法调用看java存在运行时“拆箱”行为。你们怎么看?
看下面的代码,主要是第二段代码:
  1. public class ReflectTest {
  2.         public static void main(String... args) throws Exception {
  3.                 Method method1 = ReflectTest.class.getMethod("method1", Integer.class);
  4.                 method1.invoke(new ReflectTest(), 2);
  5.                
  6.                 Method method2 = ReflectTest.class.getMethod("method2", int.class);
  7.                 method2.invoke(new ReflectTest(), 2);
  8.         }
  9.        
  10.         public void method1(Integer value){
  11.                 System.out.println(value);
  12.         }
  13.        
  14.         public void method2(int value){
  15.                 System.out.println(value);
  16.         }
  17. }
复制代码


第一个调用没什么问题。
第二个调用为方法传的第二个参数是Integer(编译时装箱),但是方法还是调用成功。所以我觉得java在通过反射调用方法的时候开始会对包装器类做一下拆箱处理的。毕竟Integer不能直接赋给int。:)




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2