Short源码详解

一、结构图

在这里插入图片描述

二、Short类介绍

Short类将基本类型short的值包装在一个对象中。

1、结构

在这里插入图片描述

2、静态属性方法介绍

// short所能存储的最小值
public static final short   MIN_VALUE = -32768;

// short所能存储的最大值
public static final short   MAX_VALUE = 32767;

// 获取short类对象
public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");

// 转换为10进制字符串
public static String toString(short s) {
    return Integer.toString((int)s, 10);
}

// 将radix进制的String类型转换为10进制的short类型
public static short parseShort(String s, int radix)
    throws NumberFormatException {
    int i = Integer.parseInt(s, radix);
    if (i < MIN_VALUE || i > MAX_VALUE)
        throw new NumberFormatException(
            "Value out of range. Value:\"" + s + "\" Radix:" + radix);
    return (short)i;
}

// 默认10进制
public static short parseShort(String s) throws NumberFormatException {
    return parseShort(s, 10);
}

// 将10进制String类型先转换为short类型,再转换为Short类型
public static Short valueOf(String s, int radix)
    throws NumberFormatException {
    return valueOf(parseShort(s, radix));
}

// 默认10进制
public static Short valueOf(String s) throws NumberFormatException {
    return valueOf(s, 10);
}

// Short也缓存了-128-127的数,其他的没有缓存。
private static class ShortCache {
    private ShortCache(){}

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

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

// 若s在缓存区间内就从缓存中取,否则将新创建一个对象。
public static Short valueOf(short s) {
    final int offset = 128;
    int sAsInt = s;
    if (sAsInt >= -128 && sAsInt <= 127) { // must cache
        return ShortCache.cache[sAsInt + offset];
    }
    return new Short(s);
}

// 解码
public static Short decode(String nm) throws NumberFormatException {
    int i = Integer.decode(nm);
    if (i < MIN_VALUE || i > MAX_VALUE)
        throw new NumberFormatException(
                "Value " + i + " out of range from input " + nm);
    return valueOf((short)i);
}

// bit位数
public static final int SIZE = 16;

// 一个short的byte数
public static final int BYTES = SIZE / Byte.SIZE;

// 将short的高8位和低8位互换,也就是两个byte互相转换
public static short reverseBytes(short i) {
    return (short) (((i & 0xFF00) >> 8) | (i << 8));
}

// 转换为无符号int类型,和Byte一样
public static int toUnsignedInt(short x) {
    return ((int) x) & 0xffff;
}

// 转换为无符号long类型,和Byte一样
public static long toUnsignedLong(short x) {
    return ((long) x) & 0xffffL;
}

3、方法介绍

// 存储short的value
private final short value;

public Short(short value) {
    this.value = value;
}

public Short(String s) throws NumberFormatException {
    this.value = parseShort(s, 10);
}

public byte byteValue() {
    return (byte)value;
}

public short shortValue() {
    return value;
}

public int intValue() {
    return (int)value;
}

public long longValue() {
    return (long)value;
}

public float floatValue() {
    return (float)value;
}

public double doubleValue() {
    return (double)value;
}

public String toString() {
    return Integer.toString((int)value);
}

public int hashCode() {
    return Short.hashCode(value);
}

public static int hashCode(short value) {
    return (int)value;
}

public boolean equals(Object obj) {
    if (obj instanceof Short) {
        return value == ((Short)obj).shortValue();
    }
    return false;
}

public int compareTo(Short anotherShort) {
    return compare(this.value, anotherShort.value);
}

public static int compare(short x, short y) {
    return x - y;
}

完结撒花★,°:.☆( ̄▽ ̄)/$:.°★

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值