<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### Integer > 本文源碼基于JDK8 Integer也是我們經常使用的工具類、包裝類,此文主要用于記錄學習筆記,主要從源碼角度深入了解一下。 ``` public final class Integer extends Number implements Comparable<Integer> { // 2147483647 public static final int MIN_VALUE = 0x80000000; // -2147483648 public static final int MAX_VALUE = 0x7fffffff; // 獲取基本類型int類型是class public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); } ``` 說明:Integer.TYPE == int.class // true * 繼承Number類 Number類中只有構造方法和幾個抽象方法: ![](https://img.kancloud.cn/3e/58/3e583f4da8f43b54f3e44e7be5f1ef10_662x326.png) ### 構造方法 ``` public Integer(int value) { this.value = value; } public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); } ``` ### 自動裝箱和自動拆箱 JDK1.5之后,java提供了自動裝箱和自動拆箱的功能,下面從源碼角度分析下Integer的裝箱: ``` package com.quancheng; public class ClassTest { public static void main(String[] args) throws InterruptedException { Integer num = 10; } } ``` 通過javap -v ClassTest.class查看字節碼,可以看出自動裝箱實際是JVM編譯器幫我們做的工作,調用的是Integer.valueOf\(\)方法 ``` public static void main(java.lang.String[]) throws java.lang.InterruptedException; descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=1, locals=2, args_size=1 0: bipush 10 2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 6: return ``` 分析到這里可以看出,實際上Integer a = 100 等價于Integer a = Integer.valueOf\(100\);只不過這個工作是JVM幫我們做的; 自動拆箱: ``` public static void main(String[] args) throws InterruptedException { int num = new Integer(11); } ``` 對應的字節碼指令: ``` public static void main(java.lang.String[]) throws java.lang.InterruptedException; descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=2, args_size=1 0: new #2 // class java/lang/Integer 3: dup 4: bipush 11 6: invokespecial #3 // Method java/lang/Integer."<init>":(I)V 9: invokevirtual #4 // Method java/lang/Integer.intValue:()I 12: istore_1 13: return ``` 通過分析可以發現,自動拆箱實際是編譯器調用了Integer.intValue\(\)的方法完成的轉換 ### 重點方法 * 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 - high之間數字的包裝類 ``` 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() {} } ``` 上面的源碼中可以看出IntegerCache有三個被final修飾的靜態filed外加一個靜態塊和一個私有的構造器;很簡單很普通的一個類,被緩存的包裝類就介于low - high之間,low的值已經寫死-128,而high的值由你的虛擬機決定sun.misc.VM.getSavedProperty\("java.lang.Integer.IntegerCache.high"\),既然是一個參數也就意味著你可以動態設置,具體怎么設置自行百度。然后在循環中將low - high之間數字的裝箱后方法cache\[\]這個Integer類型的數組中。這樣就完成了緩存 在日常編碼中,我們需要注意Integer緩存的問題 ``` Integer a = 100; Integer b = 100; Integer f = Integer.valueOf(100); Integer c = 200; Integer d = 200; System.out.println(a == b);//true System.err.println(a == f); // true System.out.println(c == d);//false ``` * stringSize\(int x\) 這個方法很有意思,我覺得可以單獨看看這個方法,主要作用就是判斷一個數字的位數,但是運用的非常巧妙 ``` final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE }; // Requires positive x static int stringSize(int x) { for (int i=0; ; i++) if (x <= sizeTable[i]) return i+1; } ``` * equals\(Object obj\) 需要注意的是Integer也重寫了equals\(Object obj\),故若比較的值類型都是Integer時,equals\(\)和==作用是相同的 ``` public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } ``` ``` 我們可以寫個例子測試下: Integer num = new Integer(999); System.err.println(num ==999); public static void main(java.lang.String[]) throws java.lang.InterruptedException; descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=2, args_size=1 0: new #2 // class java/lang/Integer 3: dup 4: sipush 999 7: invokespecial #3 // Method java/lang/Integer."<init>":(I)V 10: astore_1 11: getstatic #4 // Field java/lang/System.err:Ljava/io/PrintStream; 14: aload_1 15: invokevirtual #5 // Method java/lang/Integer.intValue:()I 18: sipush 999 21: if_icmpne 28 24: iconst_1 25: goto 29 28: iconst_0 29: invokevirtual #6 // Method java/io/PrintStream.println:(Z)V 32: return ``` 可以看出實際上是先拆箱再比較值 ### 總結 * Integer i1=40;Java在編譯的時候會直接將代碼封裝成Integer * Integer i1=Integer.valueOf\(40\);,從而使用常量池中的對象 * Integer i1 = new Integer\(40\);這種情況下會創建新的對象 * Integer的toString方法分成了兩部分進行處理,大于等于65536和小于65536的部分 ### Integer有趣示例 ``` Integer i1 = 40; Integer i2 = 40; Integer i3 = 0; Integer i4 = new Integer(40); Integer i5 = new Integer(40); Integer i6 = new Integer(0); System.out.println("i1=i2 " + (i1 == i2)); System.out.println("i1=i2+i3 " + (i1 == i2 + i3)); System.out.println("i1=i4 " + (i1 == i4)); System.out.println("i4=i5 " + (i4 == i5)); System.out.println("i4=i5+i6 " + (i4 == i5 + i6)); System.out.println("40=i5+i6 " + (40 == i5 + i6)); output==> i1=i2 true i1=i2+i3 true i1=i4 false i4=i5 false i4=i5+i6 true 40=i5+i6 true ``` 解釋:語句i4 == i5 + i6,因為+這個操作符不適用于Integer對象,首先i5和i6進行自動拆箱操作,進行數值相加,即i4 == 40。然后Integer對象無法與數值進行直接比較,所以i4自動拆箱轉為int值40,最終這條語句轉為40 == 40進行數值比較
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看