Integer类源码浅析
生活随笔
收集整理的這篇文章主要介紹了
Integer类源码浅析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、首先Integer提供了兩類工具類,包括把一個int類型轉成二進等,
? 其實執行轉換算法只有一個方法:
1 public static String toString(int i, int radix) { 2 3 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) 4 radix = 10; 5 6 /* Use the faster version */ 7 if (radix == 10) { 8 return toString(i); 9 } 10 11 char buf[] = new char[33]; 12 boolean negative = (i < 0); 13 int charPos = 32; 14 15 if (!negative) { 16 i = -i; 17 } 18 19 while (i <= -radix) { 20 buf[charPos--] = digits[-(i % radix)]; 21 i = i / radix; 22 } 23 buf[charPos] = digits[-i]; 24 25 if (negative) { 26 buf[--charPos] = '-'; 27 } 28 29 return new String(buf, charPos, (33 - charPos)); 30 }2、測試的示例代碼
1 //定義一個Integer 變量 2 Integer i = 1; 3 Integer j = 1; 4 System.out.println(i==j); //true 5 6 Integer x = 128; 7 Integer y = 128; 8 System.out.println(x==y); //false為什么會出現這樣的結果呢,因為Integer內部維護了一個緩存類IntegerCache,默認緩存-128~127的數據
1 /** 2 * Cache to support the object identity semantics of autoboxing for values between 3 * -128 and 127 (inclusive) as required by JLS. 4 * 5 * The cache is initialized on first usage. The size of the cache 6 * may be controlled by the -XX:AutoBoxCacheMax=<size> option. 7 * During VM initialization, java.lang.Integer.IntegerCache.high property 8 * may be set and saved in the private system properties in the 9 * sun.misc.VM class. 10 */IntegerCache緩存類的大小是可以設置,通過?-XX:AutoBoxCacheMax=<size> 這個選項來設置
1 private static class IntegerCache { 2 static final int low = -128; 3 static final int high; 4 static final Integer cache[]; 5 6 static { 7 // high value may be configured by property 8 int h = 127; 9 String integerCacheHighPropValue = 10 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); 11 if (integerCacheHighPropValue != null) { 12 int i = parseInt(integerCacheHighPropValue); 13 i = Math.max(i, 127); 14 // Maximum array size is Integer.MAX_VALUE 15 h = Math.min(i, Integer.MAX_VALUE - (-low) -1); 16 } 17 high = h; 18 19 cache = new Integer[(high - low) + 1]; 20 int j = low; 21 for(int k = 0; k < cache.length; k++) 22 cache[k] = new Integer(j++); 23 } 24 25 private IntegerCache() {} 26 }3、Integer類的內部維護了一個int基本類型的變量
提供了兩個構造方法來初始化,一個構造方法接受int類型,一個接受String類型,接受類型的構造方法,調用了靜態方法parseInt(s,10);
4、還有JDK1.7新添加的一個方法
1 /** 2 * Compares two {@code int} values numerically. 3 * The value returned is identical to what would be returned by: 4 * <pre> 5 * Integer.valueOf(x).compareTo(Integer.valueOf(y)) 6 * </pre> 7 * 8 * @param x the first {@code int} to compare 9 * @param y the second {@code int} to compare 10 * @return the value {@code 0} if {@code x == y}; 11 * a value less than {@code 0} if {@code x < y}; and 12 * a value greater than {@code 0} if {@code x > y} 13 * @since 1.7 14 */ 15 public static int compare(int x, int y) { 16 return (x < y) ? -1 : ((x == y) ? 0 : 1); 17 }其他方法就不做分析,可以直接參看API
?
轉載于:https://www.cnblogs.com/lianliang/p/5753891.html
總結
以上是生活随笔為你收集整理的Integer类源码浅析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RDIFramework.NET-.NE
- 下一篇: element ui中dialog相关问