Short is the wrapper class for the basic data type short.
1) Declaration Department:
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 inner class
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 ((i-128));} }
When the short class loads, the inner class is loaded, the inner class static module code executes, and the cache object array is initialized.
3) Short initialization method:
By constructor initialization, the constructor is as follows:
Constructor method overload public short (String s) throws NumberFormatException { this.value = Parseshort (S, ten); } Constructor method overload public short (short value) { this.value = value; }
By invoking the method of conversion, the series is as follows:
public static short Parseshort (String s, int radix) throws numberformatexception { int i = Integer.parseint (S, rad IX); 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); } 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); } public static short valueOf (short s) { final int offset = n; int sasint = s; if (sasint >= -128 && sasint <= 127) {//must cache return shortcache.cache[sasint + offset]; }
Observe the call relationship between the code. The first method returns a short type, and the fifth method obtains a short object by fetching the cache.
Example of initialization:
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
Conclusion: With Byte.class analysis, the short type auto-boxing will go to get the cached object ( -128~127); Initializes new with the constructor, which is an object that is not fetched from the cache.
4) Other methods
Decode, convert binary, octal, 16 binary to decimal public static short decode (String nm) in short range throws NumberFormatException {int i = Integ Er.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); The 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; The 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 and the//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 //high-low anti-regularization number short s7 = 2; Short S8 = Short.reversebytes (s7); Out.println (S8);//512 //Negative short S_3 = (short) -0b000000000000011;//symbol bit directly using symbol substitution, declaring the use of the original code, using the complement of the operation, According to the results of the operation of the complement, and then to the original code short s_4 = (short) -0b000001000000001;
5) Properties:
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
JDK Source Analysis: Short.java