我只是在探索java反射API,我遇到了以下代码片段
public class Main {
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException{
Field value=Integer.class.getDeclaredField("value");
value.setAccessible(true);
value.set(42, 43);
System.out.printf("six times seven %d%n",6*7);
System.out.printf("six times seven %d%n",42);
System.out.println(42);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
six times seven 43
six times seven 43
42
Run Code Online (Sandbox Code Playgroud)
我阅读了set方法的文档,该方法声明它为给定对象设置字段的值.但我无法理解代码的输出,因为它应该在所有情况下打印42.
任何人都可以深入了解代码中发生的事情吗?
System.out.println(42);
Run Code Online (Sandbox Code Playgroud)
正在呼唤println(int)
不println(Object)
。拳击永远不会发生。这使得它更快,并且在 1.5 之前也可以工作。
在其他情况下,您正在通过拳击Integer.valueOf(int)
。此方法定义为始终为 -128 到 127(含 -128 和 127)之间的值返回完全相同的Integer
对象(对于其他值可能有也可能没有相同的行为)。因此,无论程序中将 42 装箱何处,您都将获得相同的对象,并且当您在该对象中设置 的值时,value
无论读取哪个引用,它都会发生变化。
如果您将装箱明确地放入代码中,它将如下所示:
value.set(Integer.valueOf(42), 43);
System.out.printf("six times seven %d%n",Integer.valueOf(6*7));
System.out.printf("six times seven %d%n",Integer.valueOf(42));
System.out.println(42);
Run Code Online (Sandbox Code Playgroud)
正如我们所知,Integer.valueOf(
对于 42 返回完全相同的对象,代码实际上是:
Integer obj42 = Integer.valueOf(42);
value.set(Integer.valueOf(obj42, 43);
System.out.printf("six times seven %d%n", obj42);
System.out.printf("six times seven %d%n", obj42);
System.out.println(42);
Run Code Online (Sandbox Code Playgroud)