Byte,Short,Integer,Long类

该类里面的实现方法很简单,其中要注意到的是一个内部类

private static class ByteCache {
        private ByteCache(){}

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }

这类在编辑时就已经new好了256个Byte数据了,所以当我们需要用byte时,就要充分利用缓存来获得

看看该方法“:

    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

是直接从缓存中拿的

如果是new Byte则

   public Byte(byte value) {
        this.value = value;
    }

是一个新的


Short 跟Byte一样


Integer 与上面两者有多一些方法

转为二进制文本

 public static String toBinaryString(int i) {
        return toUnsignedString(i, 1);
    }


    /**
     * Convert the integer to an unsigned number.
     */
    private static String toUnsignedString(int i, int shift) {
        char[] buf = new char[32];
        int charPos = 32;
        int radix = 1 << shift;
        int mask = radix - 1;
        do {
            buf[--charPos] = digits[i & mask];
            i >>>= shift;
        } while (i != 0);


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

// 转换为16进制(没有前面的0x)

    public static String toHexString(int i) {
        return toUnsignedString(i, 4);
    }

// 转换为8进制

    public static String toOctalString(int i) {
        return toUnsignedString(i, 3);
    }


 public static int numberOfLeadingZeros(int i)  // 算出前导零


    public static int numberOfTrailingZeros(int i)  // 算出后导零


    public static int numberOfTrailingZeros(int i)  // 反转


Long 跟Integer 基本一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值