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的更多相关文章

  1. 【jdk源码分析】java.lang.Appendable

    1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...

  2. 【jdk源码分析】java多线程开启的三种方式

    1.继承Thread类,新建一个当前类对象,并且运行其start()方法 package com.xiaostudy.thread; /** * @desc 第一种开启线程的方式 * @author ...

  3. JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue

    JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue 目的:本文通过分析JDK源码来对比ArrayBlockingQueue 和LinkedBlocki ...

  4. JDK 源码分析(4)—— HashMap/LinkedHashMap/Hashtable

    JDK 源码分析(4)-- HashMap/LinkedHashMap/Hashtable HashMap HashMap采用的是哈希算法+链表冲突解决,table的大小永远为2次幂,因为在初始化的时 ...

  5. JDK源码分析(5)Vector

    JDK版本 Vector简介 /** * The {@code Vector} class implements a growable array of * objects. Like an arra ...

  6. JDK源码分析(2)LinkedList

    JDK版本 LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作. LinkedList 实现 ...

  7. 【JDK】JDK源码分析-HashMap(1)

    概述 HashMap 是 Java 开发中最常用的容器类之一,也是面试的常客.它其实就是前文「数据结构与算法笔记(二)」中「散列表」的实现,处理散列冲突用的是“链表法”,并且在 JDK 1.8 做了优 ...

  8. 【JDK】JDK源码分析-ArrayList

    概述 ArrayList 是 List 接口的一个实现类,也是 Java 中最常用的容器实现类之一,可以把它理解为「可变数组」. 我们知道,Java 中的数组初始化时需要指定长度,而且指定后不能改变. ...

  9. 【JDK】JDK源码分析-AbstractQueuedSynchronizer(1)

    概述 前文「JDK源码分析-Lock&Condition」简要分析了 Lock 接口,它在 JDK 中的实现类主要是 ReentrantLock (可译为“重入锁”).ReentrantLoc ...

  10. 【JDK】JDK源码分析-ReentrantLock

    概述 在 JDK 1.5 以前,锁的实现只能用 synchronized 关键字:1.5 开始提供了 ReentrantLock,它是 API 层面的锁.先看下 ReentrantLock 的类签名以 ...

随机推荐

  1. 【2017001】IList转DataTable、DataTable转IList

    IList转DataTable.DataTable转IList using System; using System.Collections.Generic; using System.Compone ...

  2. update更新修改数据

    update ---整表更新数据 update  表名  set  需要调整字段1= '值1' ,需要调整字段2= '值2'  …… ---更新条件数据 update  表名  set  需要调整字段 ...

  3. iOS合并真机和模拟器framework

    在实际的项目开发中,我们会碰到某些静态库只能在真机或者模拟器中的一个上可以运行.为了让静态库在模拟器和真机都可以正常的运行,就涉及到如何把一个工程生成的静态库打包以后生成的framework进行合并. ...

  4. 微信小程序新版用户授权方式处理

    最新更新(2018-12-27): 最近做了改版,做成默认进来就是首页,然后去判断有没有用户信息,没有的话再去判断用没授权过,如果授权过直接自动去获取,没有的话再跳转到授权页面.因为用户授权主要就是针 ...

  5. php无限级分类----封装函数

    public function catetree($cateRes){//传递过来的数据资源 return $this->sort($cateRes); 调用函数 } public functi ...

  6. python3 安装pyhanlp方法

    直接pip install pyhanlp的时候会提示缺少Microsoft Visual c++环境, 其实没有Microsoft Visual c++环境也是可以的, 可以先安装jpype1,然后 ...

  7. Redis- redis.conf

    ############################################## 基本设置 ######################################## # redis ...

  8. 解决WinScp连接被拒绝的问题

    尝试以下方法: 1) 开启|关闭防火墙(这里需要关闭) sudo ufw enable|disable 2) 开启远程服务 在终端界面输入:service sshd start.结果显示:ssh:un ...

  9. XNA+WPF solution worked

    Cory Petosky's website Edit 11/17/2010: While this article's XNA+WPF solution worked when I wrote it ...

  10. Unicode编码相关概念

    1.Unicode是一种字符映射方案,这种映射并不是编码(即还没有到二进制机器码层面),而是像一个电话本一样,把全世界所有语言使用的字符,都映射成一个"u+"开头的数字(在JAVA ...