Integer原始碼學習筆記

2021-08-15 22:28:52 字數 1866 閱讀 1038

1、  public final class integer extends number implements comparable2、tostring( )方法先得到引數i的長度,然後以該長度生成char型別的buf陣列,最後以該buf陣列作為引數呼叫new

string(buf, true)生成乙個string物件。必須先判斷integer.min_value,因為getchars()方法中使用了i=-i,以負數為基準,對於i=integer.min_value將會產生溢位。

public

static string tostring(int i)

3、呼叫integer.valueof( )方法時先判斷給定的數是否在-128~127之間(其中-128寫死,而127值可以設定),若在該範圍內,直接返回cache陣列中的integer物件(見5),否則生成乙個integer物件。

public

static integer valueof(int i)

4、覆蓋了object類中的hashcode函式,integer內部定義了final型別的int變數儲存該integer的值,該integer物件的hashcode即是其value值。

private final int

value;

public

integer(int

value)

@override

public

inthashcode()

public

static

inthashcode(int

value)

public boolean equals(object obj)

return

false;

}5、 integer內部定義了乙個私有的靜態內部類integercache,內部定義了乙個integer型別的cache陣列,若給定的數在low和high之間,直接返回該陣列中的integer物件。

private

static

class integercache catch( numberformatexception nfe)

}high = h;

cache = new integer[(high - low) + 1];

int j = low;

for(int k = 0; k < cache.length; k++)

cache[k] = new integer(j++);

assert integercache.high >= 127;

}private

integercache() {}

}6、@native public

static final int min_value = 0x80000000;

@native public

static final int max_value = 0x7fffffff;

int型別的數值範圍為 ,即-2147483648~2147483647,將0x80000000轉換成二進位制即為10000000

0000000

00000000

00000000,最高位代表符號位。單位元組有符號整數可以儲存的範圍是-128~127,正數二進位制表示範圍0000

0000~0111

1111,即1~127,負數二進位制表示範圍1000

0001 ~1111

1111,即-1~-127,還有個0分別為+0和-0, 0000

0000表示+0,1000

0000表示-128,同理可得10000000

0000000

00000000

00000000表示-2147483648。

Integer原始碼解析

public class test else integer i3 200 integer i4 200 if i3 i4 else 結果為 原因integer 類會快取 128 到 127 之間的整數 但是如果new interger的話就是不同的物件了 源 分析 如果是在 128到正的127之間...

Integer原始碼(toString)解析

public static string tostring int i 同樣,為了便於理解,我舉個具體的例子 integer.tostring 173 1 int size i 0 stringsize i 1 stringsize i 看這一行的stringsize i 方法 判斷這個value ...

JDK原始碼閱讀 Integer

先上一版字串轉數值的幾個方法的區別 parseint string s 解析字串數,10進製,返回int parseint string s,int radix 解析字串數,radix為指定進製,支援2 36進製 decode string nm 解析字串數,0開頭的為8進製,如010解析為2 0x...