jdk包装类之Short

216 阅读2分钟

常见面试题

1、short s1 = 1; s1 = s1 + 1;有错吗?那short s1 = 1; s1 += 1;是否正确? 2、Short s = new Short(1);编译是否有问题?为什么? 3、请分析下面代码块的输出值:

public static void main(String[] args) throws Exception{
    Short s = new Short("1");
    Short a = 1;
    System.out.println(s==a);
    
    Short s1 =  Short.valueOf("1");
    Short a1 = 1;
    System.out.println(s1==a1);

    Short s2 =  Short.valueOf("128");
    Short a2 = 128;
    System.out.println(s2==a2);
}

继承关系

public final class Short extends Number implements Comparable<Short>, Constable

最大值与最小值

image.png

方法列表

image.png

方法分析

构造方法

两个构造方法,传参分别是String 、short,方法在java9中开始被标记弃用。调用构造方法表示会分配内存,生成新的对象。

@Deprecated(since="9", forRemoval = true)
public Short(String s) throws NumberFormatException {
    this.value = parseShort(s, 10);
}
@Deprecated(since="9", forRemoval = true)
public Short(short value) {
    this.value = value;
}

parseShort方法

public Short(String s)可能会抛出NumberFormatException,该方法在Java9中被标记弃用。

@Deprecated(since="9", forRemoval = true)
public Short(String s) throws NumberFormatException,该方法在Java9中被标记弃用。 {
    this.value = parseShort(s, 10);
}

public static short parseShort(String s, int radix)先调用Integer.parseInt,再强转。

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;
}

valueOf方法

总的来说就是最后调用public static Short valueOf(short s),值再-128到127之前从缓存中返回值,其他的新分配对象,String 类型的参数则先转调用parseShort方法转short。

public static Short valueOf(String s) throws NumberFormatException {
    return valueOf(s, 10);
}
public static Short valueOf(String s) throws NumberFormatException {
    return valueOf(s, 10);
}
public static Short valueOf(String s, int radix)
    throws NumberFormatException {
    return valueOf(parseShort(s, radix));
}
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);
}

decode方法

先调用Integer的decode方法,后面调用valueOf方法。

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);
}

重写Number的xxxValue方法

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

@IntrinsicCandidate
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;
}

cache结构

private static class ShortCache {
    private ShortCache() {}

    static final Short[] cache;
    static Short[] archivedCache;

    static {
        int size = -(-128) + 127 + 1;

        // Load and use the archived cache if it exists
        CDS.initializeFromArchive(ShortCache.class);
        if (archivedCache == null || archivedCache.length != size) {
            Short[] c = new Short[size];
            short value = -128;
            for(int i = 0; i < size; i++) {
                c[i] = new Short(value++);
            }
            archivedCache = c;
        }
        cache = archivedCache;
    }
}

toString方法

public static String toString(short s) {
    return Integer.toString(s);
}

reverseBytes方法(java15)

/**
 * @since 1.5
 */
@IntrinsicCandidate
public static short reverseBytes(short i) {
    return (short) (((i & 0xFF00) >> 8) | (i << 8));
}

转无符号数方法

/**
 * @since 1.8
 */
public static int toUnsignedInt(short x) {
    return ((int) x) & 0xffff;
}

/**
 * @since 1.8
 */
public static long toUnsignedLong(short x) {
    return ((long) x) & 0xffffL;
}