Java源代码基础数据类型
1、Java与数先关的基础数据类型主要分整数与浮点数,整数:byte,short,int,long,浮点数:float,double
2、数据范围
类型 | 范围 |
---|---|
byte | -128~127(2^7-1) |
short | -32768~32767(2^15-1) |
int | -2147483648~2147483647(2^31-1)21亿(10位) |
long | -9223372036854775808~9223372036854775807(2^63-1)19位 |
float | +/-2^−149,+/-2^128 |
double | +/-2^−1074,+/-2^1024 |
3、六个类型均继承自Number,Number类为抽象类
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract int longValue();
public abstract int floatValue();
public abstract int doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
}
4、整数的byte、short、int、long对于的封装类,都有相关的缓存操作,具体实现是通过内部类
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
Byte对应ByteCache,Integer对应IntegerCache,Short对应ShortCache,Long对应LongCache,它们的缓存范围都是从-128~127。其中IntegerCache提供了可通过属性设置“java.lang.Integer.IntegerCache.high”设置缓存的最大值。
举个有趣的栗子
Integer c = 3;
Integer d = 3;
int e = 321;
Integer g = 321;
Integer h = 321;
// 使用了缓存
System.out.println(c == d); // true
System.out.println(e == g); // true
System.out.println(h == g); // false
5、都提供了不同进制的数表示
6、toString()
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
// 1、获取数的位数
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
// 2、int转化成char[]数组
getChars(i, size, buf);
return new String(buf, true);
}
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
//
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}
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',
} ;
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index;
char sign = 0;
if (i < 0) {
sign = '-';
i = -i;
}
// Generate two digits per iteration
while (i >= 65536) {
q = i / 100;
// really: r = i - (q * 100);
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
buf [--charPos] = DigitOnes[r];
buf [--charPos] = DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i <= 65536, i);
for (;;) {
// q = i / 10
q = (i * 52429) >>> (16+3);
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
buf [--charPos] = digits [r];
i = q;
if (i == 0) break;
}
if (sign != 0) {
buf [--charPos] = sign;
}
}
-
关键点
- 移位的效率比直接乘除的效率要高
- 乘法的效率比除法的效率要高
- 通过查找数组,实现快速访问,避免除法计算
- 重复利用计算结果:在获取r(i%100)时,充分利用了除法的结果,结合位移避免重复计算。 位运算
- 负数的补码:符号位:1,其余绝对值的原码按位取反+1
- 按位与(&)
- 按位或(|)
- 按位非(~)
- 按位异或(^):相同为0,不同为1,异或0具有保持的特点,而异或1具有翻转的特点
- 左位移(<<)理解为乘法,如900 << 6 : 900 * 2 ^ 6 = 900 * 64
- 右位移(>>)理解为除法, 如900 >> 6 : 900 / 2 ^ 6 = 900 / 64
- 无符号右移(>>>)
7、浮点数(实现的源码:FloatingDecimal),如要进行精确的运算,要使用BigDecimal