如何將兩個Integer的引用對象傳給swap方法在方法內部交換,使引用值發生變化?

首先JAVA方法中變量傳遞分引用傳遞和值傳遞兩種。基本類型是值傳遞,其餘爲引用傳遞。引用傳遞傳來的變量相當於在操作的是原變量的副本,不會對原變量造成影響。所以如果想要影響原變量,那就需要看Integer源碼是如何存儲值的。
我們使用javap -v xx.class命令反編譯class文件:

Code:
    stack=3, locals=3, args_size=1
       0: sipush        129
       3: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       6: astore_1
       7: iconst_2
       8: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: astore_2
      12: aload_1
      13: aload_2
      14: invokestatic  #3                  // Method swap:(Ljava/lang/Integer;Ljava/lang/Integer;)V
      17: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      20: new           #5                  // class java/lang/StringBuilder
      23: dup
      24: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
      27: ldc           #7                  // String after a=

可以看出 Integer a = 1, b=2 是進行了自動裝箱操作。使用了Integer.valueOf()方法。再去找下源碼:

    public static Integer valueOf(int i) {
      if (i >= IntegerCache.low && i <= IntegerCache.high)
          return IntegerCache.cache[i + (-IntegerCache.low)];
      return new Integer(i);
  }

其中 IntegerCache.low=-128,IntegerCache.high=127。也就是說在-128~127之間做了個緩存,如果賦值爲該區域間的同一值,則會使用同一地址。否則則調用構造函數,創建一個對象。(這也是在swap方法中,我們使用Integer tmp = new Integer(a);而不是直接int tmp = a的原因。否則tmp和a因爲緩存關係,會操作同一地址,當修改a值的時候,tmp的值也會改變,會出現 a=2,b=2的情況)

   private final int value;
   public Integer(int value) {
      this.value = value;
  }

可以看出,最終值是存儲在private final int value這一私有變量中的。理論上JAVA不可以更改其他類的私有變量,所以我們需要使用反射,並把setAccessible屬性設置爲true

附上實現代碼:

    public static void main(String[] args) throws Exception {
        Integer a = 1, b = 2;
        swap(a, b);
        System.out.println("after a=" + a + ", b=" + b);
    }

    public static void swap(Integer a, Integer b) throws Exception {
        Field field = Integer.class.getDeclaredField("value");
        field.setAccessible(true);
        Integer tmp = new Integer(a);
        field.set(a, b.intValue());
        field.set(b, tmp);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章