java integer源码_java Integer源码分析

======================Integer.class============================@HotSpotIntrinsicCandidatepublic static String toString(inti) {//数字长度

int size =stringSize(i);//紧凑的空间布局(COMPACT_STRINGS),LATIN1单字节编码,UTF16双字节编码

if(COMPACT_STRINGS) {byte[] buf = new byte[size]; //单字节编码,一个字节存储一个数字

getChars(i, size, buf);return newString(buf, LATIN1);

}else{byte[] buf = new byte[size * 2]; //双字节编码,两个字节存储一个数字

StringUTF16.getChars(i, size, buf);return newString(buf, UTF16);

}

}==================Integer.class=======================

//int类型数字位数//int类型范围[-2147483648, 2147483647]//最高10位数。//正数长度最高是10,负数长度最高是11(比正数多一位负号)

static int stringSize(intx) {int d = 1;if (x >= 0) {

d= 0;

x= -x;

}int p = -10;for (int i = 1; i < 10; i++) {if (x >p)return i +d;

p= 10 *p;

}return 10 +d;

}================Integer.class========================

static int getChars(int i, int index, byte[] buf) {intq, r;int charPos =index;boolean negative = i < 0;//转为负数参加下面的计算

if (!negative) {

i= -i;

}//Generate two digits per iteration//每次迭代生成两位数,并通过两个字符数组DigitOnes、DigitTens获取十位数和个位数所对应的字符。//从后到前填充buf。

while (i <= -100) {

q= i / 100;//取出该数字的最低两位

r = (q * 100) -i;

i=q;//个位对应的字符

buf[--charPos] =DigitOnes[r];//十位对应的字符

buf[--charPos] =DigitTens[r];

}//We know there are at most two digits left at this point.//经过前面的迭代后,0 > i > -100,

q = i / 10;//取数字i的个位(i可能是两位数或个位数)

r = (q * 10) -i;//通过偏移量r取数字r对应的字符'r'

buf[--charPos] = (byte)('0' +r);//Whatever left is the remaining digit.//如果前面的i是两位数,则q是i的十位数,且q<0//如果前面的i是一位数,则q==0,q为0则不用将q放入buf

if (q < 0) {

buf[--charPos] = (byte)('0' - q); //q为负数,计算绝对值|q|对应的'q'时用'0'-q。

}//若是负数,加上负号

if(negative) {

buf[--charPos] = (byte)'-';

}returncharPos;

}======================StringUTF16.class=======================================

static int getChars(int i, int index, byte[] buf) {intq, r;int charPos =index;boolean negative = (i < 0);if (!negative) {

i= -i;

}//Get 2 digits/iteration using ints

while (i <= -100) {

q= i / 100;

r= (q * 100) -i;

i=q;

putChar(buf,--charPos, Integer.DigitOnes[r]);

putChar(buf,--charPos, Integer.DigitTens[r]);

}//We know there are at most two digits left at this point.

q = i / 10;

r= (q * 10) -i;

putChar(buf,--charPos, '0' +r);//Whatever left is the remaining digit.

if (q < 0) {

putChar(buf,--charPos, '0' -q);

}if(negative) {

putChar(buf,--charPos, '-');

}returncharPos;

}========================StringUTF16.class=============================@HotSpotIntrinsicCandidate//intrinsic performs no bounds checks

static void putChar(byte[] val, int index, intc) {assert index >= 0 && index < length(val) : "Trusted caller missed bounds check";

index<<= 1; //和 index*2 等价,用来确定c在val中存储的起始位置,字节长度是数字字符串长度的两倍//val中两个字节存储c(00000000 xxxxxxxx)。//UTF-16中有字节序的概念,存储的时候,要区分子节序,即大小端//HI_BYTE_SHIFT和LO_BYTE_SHIFT其中一个是8,一个是0//取c的高8位或低8位存储在val的低地址或高地址,达到大小端存储的目的。

val[index++] = (byte)(c >>HI_BYTE_SHIFT);

val[index]= (byte)(c >>LO_BYTE_SHIFT);

}======================StringUTF16.class 大小端(字节序)==============================================

private static native boolean isBigEndian(); //大端

static final intHI_BYTE_SHIFT;static final intLO_BYTE_SHIFT;static{if(isBigEndian()) {

HI_BYTE_SHIFT= 8;

LO_BYTE_SHIFT= 0;

}else{

HI_BYTE_SHIFT= 0;

LO_BYTE_SHIFT= 8;

}

}==============Integer.class===============================

//获取两位数10*a+b其十位(a)、个位(b)对应的字符'a'、'b'。//如下获取十位所对应的字符://byte i = (byte)10*a+b;//a,b属于10以内的数//DigitTens[i] == a;//true

static final byte[] 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',

} ;static final byte[] 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',

} ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值