JDK源码分析:Short.java

Posted 君子藏器于身,伺时而动。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK源码分析:Short.java相关的知识,希望对你有一定的参考价值。

  Short是基本数据类型short的包装类。

  1)声明部:

public final class Short extends Number implements Comparable<Short> 

  extends Number,override methods:

public abstract int intValue();    
public abstract float floatValue();    
public abstract long longValue();    
public abstract double doubleValue();    
public byte byteValue() {    
    return (byte)intValue();    
}    
public short shortValue() {    
    return (short)intValue();    
}

  implements Comparable<Short> :

public int compareTo(Short anotherShort) {
        return compare(this.value, anotherShort.value);
    }
public static int compare(short x, short y) {
        return x - y;
    }

  2)私有静态内部类

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

  Short类加载的时候,加载该内部类,内部类静态模块代码执行,初始化缓存对象数组。

  3)Short初始化方法:

  通过构造函数初始化,构造函数如下:

//构造函数方法重载  
public Short(String s) throws NumberFormatException {  
    this.value = parseShort(s, 10);  
}  
//构造函数方法重载  
public Short(short value) {  
    this.value = value;  
}

  通过调用转换的方法,该系列方法如下:

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

  观察代码之间的调用关系。第一个方法返回short类型,第五个方法通过取缓存获得Short对象。

  初始化例子:

short  s_1 = 1;  
String str_1 = "1";  
Short s1 = new Short(str_1);  
Short s2 = new Short(s_1);  
Short s3 = s_1;  
Short s4 = Short.parseShort(str_1);  
Short s5 = Short.valueOf(str_1);  
s1 == s2;//fasle  
s1 == s3;//fasle  
s2 == s3;//fasle  
s4 == s3;//true  
s5 == s3;//true

  结论:同Byte.class分析,short类型自动装箱会去获取缓存的对象(-128~127);使用构造函数初始化new,是一个新的对象,不从缓存里去获取对象。

  4)其他方法

//解码,将short范围内的二进制,八进制,十六进制转换为十进制  
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);  
}  
  
public static String toString(short s) {  
    return Integer.toString((int)s, 10);  
}  
public String toString() {  
    return Integer.toString((int)value);  
}  
  
@Override  
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 static int toUnsignedInt(short x) {  
    return ((int) x) & 0xffff;  
}  
public static long toUnsignedLong(short x) {  
    return ((long) x) & 0xffffL;  
}  
//Returns the value obtained by reversing the order of the bytes in the two‘s   
//complement representation of the specified {@code short} value.  
public static short reverseBytes(short i) {  
    return (short) (((i & 0xFF00) >> 8) | (i << 8));  
}  

  e.g:

short  s_1 = 1;  
short s_2 = -1;  
String str_2 = "0x21";  
Short s6 = Short.decode(str_2);//33  
Out.println(s6.toString());//33  
Out.println(Short.toString(s6.shortValue()));//33  
Out.println(s6.hashCode());//33  
Out.println(Short.toUnsignedInt(s_1));//1  
Out.println(Short.toUnsignedInt(s_2));//65535  
Out.println(Short.toUnsignedLong(s_1));//1  
Out.println(Short.toUnsignedLong(s_2));//65535  
Out.println(s4.equals(s1));//true  
//高位低位反转 正数  
Short s7 = 2;  
Short s8 = Short.reverseBytes(s7);  
Out.println(s8);//512  
//负数  
short s_3 = (short)-0B000000000000011;//符号位直接使用符号替代,声明使用原码,运算时候使用补码,根据运算结果得出补码,再转为原码  
short s_4 = (short)-0B000001000000001;  
Out.println(s_4 == Short.reverseBytes(s_3));//true 

  5)属性:

Out.println("MAX:" + Short.MAX_VALUE);  
Out.println("MIN:" + Short.MIN_VALUE);  
Out.println("BYTES:" + Short.BYTES);  
Out.println("bit size:" + Short.SIZE);  
Out.println("primitive type:" + Short.TYPE);  
  
MAX:32767  
MIN:-32768  
BYTES:2  
bit size:16  
primitive type:short 

  

以上是关于JDK源码分析:Short.java的主要内容,如果未能解决你的问题,请参考以下文章

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段

Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

JDK源码分析-FutureTask

jdk源码分析——String类

JDK源码阅读之 HashMap