返回

java.lang.Short类型揭秘:从源码看简便的整数包装类

后端

Java基础库之Short 源码解读:简单整洁,学之便懂!

一、认识Short类

在Java基础库中,Short类是一个简单的整数包装类,它允许将基本数据类型short包装成对象。包装类的主要目的是将基本数据类型转换为对象,以便能够使用面向对象编程语言的特性,例如继承、多态和异常处理。

二、Short类的源码解析

  1. 类声明
public final class Short extends Number implements Comparable<Short> {
    /**
     * Constant for the maximum {@code short} value, 2<sup>15</sup>-1.
     */
    public static final short MAX_VALUE = 32767;

    /**
     * Constant for the minimum {@code short} value, -2<sup>15</sup>.
     */
    public static final short MIN_VALUE = -32768;
}

Short类的声明十分简单,它继承自Number类,并实现了Comparable接口,这意味着Short类可以与其他Short对象进行比较。类中还定义了两个常量:MAX_VALUE和MIN_VALUE,分别表示short类型的最大值和最小值。

  1. 构造方法
    /**
     * Constructs a newly allocated {@code Short} object that represents the
     * specified {@code short} value.
     *
     * @param value the value to be represented by the {@code Short}.
     */
    public Short(short value) {
        this.value = value;
    }

    /**
     * Constructs a {@code Short} object representing the {@code short}
     * value represented by the string. The string is converted to a {@code
     * short} value as if by the {@link #parseShort} method.
     *
     * @param s the string to be converted to a {@code Short}.
     * @throws NumberFormatException if the string does not contain a
     * parsable {@code short}.
     * @see #parseShort(String)
     */
    public Short(String s) throws NumberFormatException {
        this.value = Short.parseShort(s, 10);
    }
}

Short类提供了两个构造方法:一个是将short值包装成Short对象,另一个是将字符串转换为Short对象。

  1. 常用方法
    /**
     * Compares two {@code Short} objects numerically.
     *
     * @param anotherShort the {@code Short} to be compared.
     * @return the value {@code 0} if this {@code Short} is equal to the
     * argument {@code Short}; a value less than {@code 0} if this {@code
     * Short} is numerically less than the argument {@code Short}; and a
     * value greater than {@code 0} if this {@code Short} is numerically
     * greater than the argument {@code Short} (signed comparison).
     * @since 1.2
     */
    public int compareTo(Short anotherShort) {
        return compare(value, anotherShort.value);
    }

    /**
     * Converts the specified string to a {@code short} value. The ASCII
     * characters {@code '0'} through {@code '9'} are interpreted as
     * representing their corresponding digits, with leading characters
     * {@code '+'} and {@code '-'} indicating the sign of the number. The
     * resulting integer value is returned.
     *
     * <p>An exception is thrown if the string does not contain a
     * parsable {@code short}, or the value represented by the string is
     * less than {@link Short#MIN_VALUE} or greater than {@link
     * Short#MAX_VALUE}.
     *
     * @param s the string to be parsed.
     * @return the {@code short} value represented by the string.
     * @throws NumberFormatException if the string does not contain a
     * parsable {@code short}.
     */
    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 10);
    }
}

Short类提供了多种常用方法,包括比较方法compareTo、类型转换方法parseShort等。

三、总结

Short类是Java基础库中一个简单而实用的整数包装类,它允许将基本数据类型short包装成对象,并提供了丰富的操作方法。通过阅读Short类的源码,我们可以更深入地了解Java基础库的设计与实现,以便更好地掌握Java编程语言。