Java源碼——Integer

來源:互聯網
上載者:User

標籤:new   水平   轉換   英語   最小   指定   bool   dig   符號   

  

  最近在研究java的原始碼,但是由於自己英語水平有限,所以想使用中文注釋的方式把源碼裡的方法全部重寫

    一遍,下面是樓主整理出來的一小部分。我把整體的項目託管到GitHub上了,歡迎大家前去交流學習。

    GitHub : https://github.com/15128928804/yuanMa

 

  

/**
* @Author:zhuangfei
* @Description:返回由第二個參數指定基數轉換為字串格式的第一個參數
* 如果基數超過了Character的最小(-2)或最大(36)區間,會指定為10
* 如第一個參數為負,則會把它相應轉換後的ASCII參數前加上 ‘-’
* i :需要轉換的參數
* radix :指定的基數
* @Date:11:13 2017/11/29
*/
public static String toString(int i, int radix) {
if(radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
radix = 10;
}

if(radix == 10)
return toString(i);

char buf[] = new char[33];
boolean negative = (i < 0);
int charPos = 32;

if(!negative)
i = -i;

while (i <= -radix) {
buf[charPos--] = digits[-(i % radix)];
i = i / radix;
}
buf[charPos] = digits[-i];
if(negative) {
buf[--charPos] = ‘-‘;
}

return new String(buf, charPos, (33 - charPos));
}

/**
* @Author:zhuangfei
* @Description:返回指定參數的String格式,指定的整數參數轉換為有符號的小數返回
* i :指定參數
* @Date:11:28 2017/11/29
*/
public static String toString(int i) {
if(i == Integer.MIN_VALUE) {
return "-2147483648";
}
int size = (i < 0)? stringSize(-i) + 1: stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}

/**
* @Author:zhuangfei
* @Description:將整數放入數組中,字元被放置到緩衝區裡,然後從指定索引處最不重要
* 的數開始向後遍曆
* i :整數
* index :指定的索引
* buf :字元數組
* @Date:11:33 2017/11/29
*/
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index;
char sign = 0;

if (i < 0) {
sign = ‘-‘;
i = -i;
}

while (i >= 65536) {
q = i / 100;
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];
}

for(;;) {
q = (i * 52429) >>> (16 + 3);
r = i - ((q << 3) + (q << 1));
buf[--charPos] = digits[r];
i = q;
if (i == 0) break;
}
if(sign != 0) {
buf[--charPos] = sign;
}
}
/**
* @Author:zhuangfei
* @Description:需要正數的參數
* x :正參
* @Date:11:42 2017/11/29
*/
static int stringSize(int x) {
for(int i = 0; ; i++) {
if(x <= sizeTable[i]) {
return i + 1;
}
}
}

Java源碼——Integer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.