大神请指教 一个和引用 ,包装类有关的问题
public class Test {
public static void main(String[] args) {
Integer num = new Integer(1);
System.out.println(num);
fun(num);
System.out.println(num);
}
public static void fun(Integer num){
num.valueOf(3);
}
import java.lang.reflect.Field;
public class Test2 {
public static void main(String[] args) {
Integer num = new Integer(1);
System.out.println(num);
fun(num);
System.out.println(num);
}
public static void fun(Integer num) {
Class clazz = num.getClass();
try {
Field field = clazz.getDeclaredField("value");
field.setAccessible(true);
field.set(num, 3);
field.setAccessible(false);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}