1. 程式人生 > >JDK源碼分析:Short.java

JDK源碼分析:Short.java

調用 rim strac 重載 八進制 t對象 ext i++ nal

  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