Java源代码之路---基础数据类型

本文探讨了Java中的基础数据类型,包括整数类型(byte, short, int, long)和浮点数类型(float, double)。这些类型均继承自抽象类Number。特别地,整数类型的封装类(Byte, Integer, Short, Long)利用内部类实现缓存,缓存范围一般为-128到127。此外,文章还提及了不同进制表示和浮点数的ToString方法,以及在需要精确运算时使用BigDecimal的重要性。" 117344358,10540465,Android VideoView 音量控制与反射技术应用,"['Android开发', '多媒体处理', '反射技术']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值