您的位置:首页 > 编程语言 > Java开发

Java8 基础数据类型包装类-Short

2018-01-10 10:01 246 查看

基础

//final修饰不可更改,每次赋值都是新建类(其中-128~127是通过ShortCache获取的不是新建的,可以使用==比较,但其他数据是通过new新建的,不能使用==直接比较大小,因为是不同的类,地址不同,需用equals比较)
public final class Short extends Number implements Comparable<Short> {}


常量

//-2^15
public static final short   MIN_VALUE = -32768;
//2^15-1
public static final short   MAX_VALUE = 32767;
//16位
public static final int SIZE = 16;
//两个字节大小
public static final int BYTES = SIZE / Byte.SIZE;


继承

抽象类Number

获取包装类与基本类型之间的转换值,short、int、long、byte、float、double。

实现

Comparable 接口

实现compareTo(T o)方法

私有静态内部类

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,Short转String

//调用Integer的toString方法,s为10进制数
public static String toString(short s) {}
//调用Integer的toString方法,s为10进制数
public String toString() {}


String转short,Short

//调用integer的parseInt方法,然后比较大小,在short范围的进行强转,否则抛异常
//s:数值型字符串
//radix:s字符串是几进制
public static short parseShort(String s, int radix) throws NumberFormatException {}
//默认s字符串是10进制
public static short parseShort(String s) throws NumberFormatException {}
//如果s范围在-128~127之间则直接取ShortCache.cache数组中的值,否则new
public static Short valueOf(short s) {}
//return valueOf(parseShort(s, radix));
public static Short valueOf(String s, int radix) throws NumberFormatException {}
//默认10进制
public static Short valueOf(String s) throws NumberFormatException {}
//调用Integer.decode(),在判断数字大小,满足条件进行强制类型转换
public static Short decode(String nm) throws NumberFormatException {}


Short转基本数据类型

public byte byteValue() {}
public short shortValue() {}
public int intValue() {}
public long longValue() {}
public float floatValue() {}
public double doubleValue() {}
//无符号x转int
public static int toUnsignedInt(short x) {}
//无符号x转long
public static long toUnsignedLong(short x) {}


比较

//返回类型为int,结果为x-y的值。与integer的compare不一样,integer返回-1,1,0
public int compareTo(Short anotherShort) {}
//返回类型为int,结果为x-y的值。与integer的compare不一样,integer返回-1,1,0
public static int compare(short x, short y) {}


位运算

//例:1101->1101000...(将前面的0补到后面)
public static short reverseBytes(short i) {}


4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息