标签:click final 作用 new onclick 第一个 改变 illegal show
1 private static void swap1(Integer i, Integer j) { 2 int tmp = i; 3 i = j; 4 j = tmp; 5 }
调用之后发现,并没有像我们预料的那样起作用,两个值没有交换;
private static void swap3(Integer i, Integer j) throws NoSuchFieldException, IllegalAccessException { Integer tmp = i; Field field = Integer.class.getDeclaredField("value"); field.setAccessible(true); field.set(i,j.intValue()); field.set(j,tmp); }
在Integer中保存值的是
private final int value;
private的变量正常应该是访问不到的,所以要用field.setAccessible(true);来暴力访问
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
从-128到127之间的值都会被Integer缓存起来,如果在这个范围内,会直接返回缓存的值;
Integer tmp = new Integer(i.intValue());然后就没有问题了。
标签:click final 作用 new onclick 第一个 改变 illegal show
原文地址:https://www.cnblogs.com/lanhaiyue/p/11055026.html