日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

number 限制最长数字_Java源码阅读-Number

發布時間:2023/12/10 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 number 限制最长数字_Java源码阅读-Number 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

抽象類Number是BigDecimal, BigInteger,Byte,Double,Float,Integer, Long, Short類的父類,實現了java.io.Serializable接口,這個接口以后再說。其中的抽象方法主要是基本數據類型的轉換,這些方法都可能涉及到四舍五入和截斷

Integer類

實現了父類的抽象方法,利用的機制是強制類型轉換。

  • 成員變量
public static final int MIN_VALUE = 0x80000000; public static final int MAX_VALUE = 0x7fffffff;

最大值和最小值,大小都是32位int的最大值和最小值

public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

類實例的基本類型

final static char[] digits = {'0' , '1' , '2' , '3' , '4' , '5' ,'6' , '7' , '8' , '9' , 'a' , 'b' ,'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,'o' , 'p' , 'q' , 'r' , 's' , 't' ,'u' , 'v' , 'w' , 'x' , 'y' , 'z'};

所有可能的字母,在數字轉String的時候用到

final static char [] DigitTens = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0','1', '1', '1', '1', '1', '1', '1', '1', '1', '1','2', '2', '2', '2', '2', '2', '2', '2', '2', '2','3', '3', '3', '3', '3', '3', '3', '3', '3', '3','4', '4', '4', '4', '4', '4', '4', '4', '4', '4','5', '5', '5', '5', '5', '5', '5', '5', '5', '5','6', '6', '6', '6', '6', '6', '6', '6', '6', '6','7', '7', '7', '7', '7', '7', '7', '7', '7', '7','8', '8', '8', '8', '8', '8', '8', '8', '8', '8','9', '9', '9', '9', '9', '9', '9', '9', '9', '9',} ;final static char [] DigitOnes = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9',} ;

這兩組變量是String轉數字的時候使用,當數字大于65536時,一次轉換兩位數字,DigitTens 是十位數,DigitOnes 是個位數

final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,99999999, 999999999, Integer.MAX_VALUE };

此變量是用于判斷數字的位數,當小于等于sizeTable[i]時,位數為i+1

private final int value;

類實例中存儲數據的成員變量

public static final int SIZE = 32;

記錄了最長位數的Integer

  • 構造函數

有兩個構造函數,分別是:

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

參數為一個int,賦值給value,另一個構造函數為

public Integer(String s) throws NumberFormatException {this.value = parseInt(s, 10);}

參數為String,調用本類中的parseInt以默認10進制的方式解析成int類型數據,再賦值。

  • 其他函數
public static int parseInt(String s, int radix)throws NumberFormatException {...return negative ? result : -result; }public static int parseInt(String s) throws NumberFormatException {return parseInt(s,10); }

參數有兩個,s:被解析的字符串,radix:進制(默認是10進制)。排除掉所有的異常后,實現的細節是先取得字符轉是否有負號,遍歷字符串,得到result,最后加上負號。

public static String toString(int i, int radix) {...if (radix == 10) {return toString(i);}...return new String(buf, charPos, (33 - charPos)); } public static String toString(int i) {...return new String(buf, true); } public String toString() {return toString(value); }

轉字符串的函數,傳入的參數還是有進制,當進制為10時,調用另一個重載函數,無參數的函數返回的是value的10進制形式。

private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = Math.min(i, Integer.MAX_VALUE - (-low) -1);}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);}private IntegerCache() {}}

有一個靜態嵌套類,定義的是一個緩存,默認值為[-128,127],可以通過設置JVM的參數修改這個緩存區間的最大值,緩存中會提前新建好數組存儲緩存區間的對象。在進行解碼String的時候,是調用了緩存的,除此之外,什么地方還用到了這個緩存呢?

當我們直接使用整數創建Integer對象時,調用的是valueOf函數,如下

Integer a = 100;//調用valueOf函數創建對象,可以反匯編、設斷點去看 public final class Integer extends Number{public static Integer valueOf(int i) {assert IntegerCache.high >= 127;if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);} }

當a的值在緩存區間的時候,那么直接返回緩存的對象,而且下次定義b也等于100的話,a==b的結果是true,因為使用的是同一個對象,如果a和b的值在緩存區間外的話,a==b的結果是false,當然可以通過JVM的參數,設置這個緩存區間。

Short類、Byte類、Long類

都跟Integer很像,其中不少方法也是借鑒了Integer類中的方法去實現,其中一個比較重要的點是,他們中的緩存是固定長度的,是直接寫死的。

Float類、Double類

與之前的實現細節是一樣的,只是多了很多對浮點的處理

閱讀源碼為JDK1.7

總結

以上是生活随笔為你收集整理的number 限制最长数字_Java源码阅读-Number的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。