JDK源碼分析:Short.java

來源:互聯網
上載者:User

標籤:ble   源碼分析   AC   pareto   instance   object   exce   returns   進位   

  Short是基礎資料型別 (Elementary Data Type)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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.