考点:Integer 用final修饰 + 缓存机制 代码: public static void swap(Integer a, Integer b) throws NoSuchFieldException, IllegalAccessException { Field valueField = Integer.class.getDeclaredField("value"); valueField.setAccessible(true); // 创建临时存储的基本类型值 int tempA = a; int tempB = b; // 直接设置基本类型值 valueField.setInt(a, tempB); valueField.setInt(b, tempA); } public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { Integer a = 1; Integer b = 2; swap(a, b); System.out.println(a + " " + b); }
2 1

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务