1. 程式人生 > >[java] Integer類解析

[java] Integer類解析

本篇文章基於jdk1.8

概述

Integer類定義如下:

public final class Integer extends Number implements Comparable<Integer> {

}

它是一個final修飾的類,意味著它不可被繼承,也提供了相對比較完善的功能。

Integer類是原始型別int的包裝類,一個Integer類的物件包含有一個單獨的型別是int的欄位。這個類提供了幾種將int轉換為String和String轉換為int的方法,以及處理int時有用的其他常量和方法。

bit twiddling方法比如highestOneBit(int)方法和numberOfTrailingZeros(int)方法的實現都是基於 Henry S. Warren, Jr.'s提供的材料

Integer類的一個成員變數是:

  /**
     * The value of the {@code Integer}.
     *
     * @serial
     */
    private final int value;

這個成員變數value即是Integer類的int值的儲存變數。

Integer類的最大最小值定義如下

/**
		 * 最小值:-2147483648
		 */
		@Native
		public static final int MIN_VALUE = 0x80000000;
 
		/**
		 * 最大值:2147483647
		 */
		@Native
		public static final int MAX_VALUE = 0x7fffffff;

parseInt方法

parseInt方法原始碼當中最重要的實現邏輯如下:
        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;

可以看到實現的方法是通過String類的charAt方法去獲取字元,然後傳入類Character的digit方法當中。

equals方法

Integer類重寫了equals方法,實現如下:

 public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

即比較兩個Integer物件的int值是否相等。

Integer類的快取實現

在看Integer類的快取之前不妨先來看一段程式碼


public class IntegerTest {
    public static void main(String[] args) {
        Integer int1 = Integer.valueOf("100");
        Integer int2 = Integer.valueOf("100");
        System.out.println(int1 == int2);

        Integer int3 = Integer.valueOf("1000");
        Integer int4 = Integer.valueOf("1000");
        System.out.println(int3 == int4);
    }
}

這段程式碼的列印結果為:

true

false

第二個列印結果為false可以理解,畢竟比較的是物件,那麼為什麼第一個比較的結果卻是true呢,答案就在Integer類的快取裡面。在Integer類當中有一個內部類用於實現快取。

 /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

以上程式碼的作用就是,使用了一個靜態cache陣列,初始化時陣列將一定範圍的整數放到cache陣列中。因此在使用Integer類的時候,它已經把-128到127的整數都提前例項化了。 這就解釋了上面的例子程式的結果,不管建立多少個這個範圍內的Integer用ValueOf出來的都是同一個物件。

因為ValueOf方法的實現就用到了這個快取,如下:

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

一目瞭然,若傳入的引數i在cahce的最大值和最小值之間,那麼就直接返回cache當中的值,省去了例項化的步驟。

bitCount方法

該方法的目的是就算一個十進位制的數對應的二進位制數當中1的個數,實現如下:

public static int bitCount(int i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}

關於它的解釋可以參考:https://blog.csdn.net/qq_33516547/article/details/78156879?locationNum=9&fps=1