一、Number
java中的数值类都继承了java.lang.Number
byte byteValue() //返回指定号码作为值 byte ,这可能涉及舍入或截断。
abstract double doubleValue()//返回指定数字的值为 double ,可能涉及四舍五入。
abstract float floatValue()//返回指定数字的值为 float ,可能涉及四舍五入。
abstract int intValue()//返回指定号码作为值 int ,这可能涉及舍入或截断。
abstract long longValue()//返回指定数字的值为 long ,可能涉及四舍五入或截断。
short shortValue()//返回指定号码作为值 short ,这可能涉及舍入或截断。
Java基本类型的包装类的大部分都实现了常量池技术,即
Byte,Short,Integer,Long,Character,Boolean;前面4种包装类默认创建了数值【-128,127】的相应类型的缓存数据,Character创建了数值在【0,127】范围的缓存数据,Boolean直接返回True Or False。如果超出对应范围仍然会去创建新的对象。
二、Integer
1、缓存
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
int h = 127;
String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
Integer会将[-128, 127]的Integer缓存起来,所以Integer.valueOf(127)==Integer.valueOf(127),值是true
,因为valueOf底层会判断这个值是否在[-128, 127]间,如果在就到缓存中取值。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
注意:new Integer(1) == new Integer(1)
值是false,因为他们没有到缓存中取值
2、数学计算
static int max(int a, int b)
static int min(int a, int b)
static int sum(int a, int b)
//返回dividend%divisor的无符号余数
static int remainderUnsigned(int dividend, int divisor)
//返回dividend/divisor的无符号商
static int divideUnsigned(int dividend, int divisor)
static int compare(int x, int y)//如果x>y返回1
static int compareUnsigned(int x, int y) //忽略符号比较
int compareTo(Integer anotherInteger)//如果当前值大于anotherInteger返回1
3、变为Integer
static Integer valueOf(int i)
static Integer valueOf(String s)//s不能为null
static Integer valueOf(String s, int radix)//radix是以几进制解析
//有符号解析,值可以是负数
static int parseInt(String s)//十进制解析
static int parseInt(String s, int radix)//radix是以几进制解析
//无符号解析,值不可以是负数
static int parseUnsignedInt(String s)//十进制解析
static int parseUnsignedInt(String s, int radix)//radix是以几进制解析
4、变为String
static String toString(int i)
static String toString(int i, int radix)//radix是以几进制解析
static String toUnsignedString(int i, int radix) //radix是以几进制解析
static String toBinaryString(int i)//二进制解析
static String toHexString(int i)//十六进制解析
static String toOctalString(int i)//八进制解析
static String toUnsignedString(int i)//十进制解析
static Integer decode(String nm)
5、变为hashCode
int hashCode()//底层就是返回Integer的value
static int hashCode(int value)
6、二进制处理
static int lowestOneBit(int i) //最低位1的位置
static int highestOneBit(int i)
static int bitCount(int i)//返回指定 int 值的二进制补码表示形式的 1 位的数量。
static int numberOfLeadingZeros(int i)//在指定 int 值的二进制补码表示形式中最高位(最左边)的 1 位之前,返回零位的数量。
static int numberOfTrailingZeros(int i)//返回指定的 int 值的二进制补码表示形式中最低(“最右边”)的为 1 的位后面的零位个数。
static int reverse(int i)//返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。
static int reverseBytes(int i)//返回通过反转指定 int 值的二进制补码表示形式中字节的顺序而获得的值。
static int rotateLeft(int i, int distance) //返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。
static int rotateRight(int i, int distance)//返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。
7、系统属性
static Integer getInteger(String nm) 确定具有指定名称的系统属性的整数值。
static Integer getInteger(String nm, int val)确定具有指定名称的系统属性的整数值。
static Integer getInteger(String nm, Integer val)返回具有指定名称的系统属性的整数值。
三、Long
1、缓存
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
Long会将[-128, 127]的Long缓存起来,所以Long.valueOf(127)==Long.valueOf(127),值是true
,因为valueOf底层会判断这个值是否在[-128, 127]间,如果在就到缓存中取值。
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
2、数学计算
static long sum(long a, long b)
static long max(long a, long b)
static long min(long a, long b)
static int compare(long x, long y)//x大返回1
int compareTo(Long anotherLong) //当前值大返回 1
static int compareUnsigned(long x, long y) //忽略符号比较
//返回dividend/divisor的无符号商
static long divideUnsigned(long dividend, long divisor)
//返回dividend%divisor的无符号余数
static long remainderUnsigned(long dividend, long divisor)
3、变为Long
static Long valueOf(long l)
static Long valueOf(String s)
static Long valueOf(String s, int radix)//radix是进制
static Long decode(String nm)
static long parseLong(String s)//十进制解析,可以是负数
static long parseLong(String s, int radix)//radix是进制
static long parseUnsignedLong(String s)//十进制解析,非负数解析
static long parseUnsignedLong(String s, int radix)//radix是进制,非负数解析
4、变为String
static String toBinaryString(long i)//二进制
static String toHexString(long i)//16进制
static String toOctalString(long i)//8进制
String toString()
static String toString(long i)
static String toString(long i, int radix)//radix指定进制
static String toUnsignedString(long i)//十进制解析非负数,如果是负数报错
static String toUnsignedString(long i, int radix)//指定进制解析非负数,如果是负数报错
5、变为hashCode
boolean equals(Object obj)//比较的是value值
int hashCode()//hashCode是value值
static int hashCode(long value)
6、二进制处理
static int bitCount(long i)//返回指定 long 值的二进制补码表示形式中的 1 位的数量。
static long highestOneBit(long i) //返回至多有一个 1 位的 long 值,在指定的 long 值中最高位(最左边)的 1 位的位置。
static long lowestOneBit(long i) //返回至多有一个 1 位的 long 值,在指定的 long 值中最低位(最右边)的 1 位的位置。
static int numberOfLeadingZeros(long i)//在指定 long 值的二进制补码表示形式中最高位(最左边)的 1 位之前,返回零位的数量。
static int numberOfTrailingZeros(long i) //返回在指定 long 值的二进制补码表示形式中最低位(最右边)的 1 位之后的零位的数量。
static long reverse(long i)//返回通过反转指定 long 值的二进制补码表示形式中位的顺序而获得的值。
static long reverseBytes(long i)//返回通过反转指定 long 值的二进制补码表示形式中字节的顺序而获得的值。
static long rotateLeft(long i, int distance)//返回根据指定的位数循环左移指定的 long 值的二进制补码表示形式而得到的值。
static long rotateRight(long i, int distance)//返回根据指定的位数循环右移指定的 long 值的二进制补码表示形式而得到的值。
7、系统属性
static Long getLong(String nm)//确定 long具有指定名称的系统属性的值。
static Long getLong(String nm, long val)//确定 long具有指定名称的系统属性的值。
static Long getLong(String nm, Long val)//以指定的 long返回系统属性的 long值。
四、 Byte
1、缓存
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
Byte会将[-128, 127]的Long缓存起来,所以Byte.valueOf((byte)127)==Byte.valueOf((byte)127),值是true
,因为valueOf底层会判断这个值是否在[-128, 127]间,如果在就到缓存中取值。
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
2、数学计算
static int compare(byte x, byte y)
int compareTo(Byte anotherByte)
3、变为Byte
static Byte valueOf(byte b)
static Byte valueOf(String s)
static Byte valueOf(String s, int radix)
static Byte decode(String nm)
static byte parseByte(String s)//十进制解析
static byte parseByte(String s, int radix)
4、变为String
String toString()
static String toString(byte b)
5、hashCode
boolean equals(Object obj)
int hashCode()
static int hashCode(byte value)
五、Short
1、缓存
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会将[-128, 127]的Long缓存起来,所以Short.valueOf((short)127)== Short.valueOf((short)127),值是true
,因为valueOf底层会判断这个值是否在[-128, 127]间,如果在就到缓存中取值。
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);
}
2、数学计算
static int compare(short x, short y)
int compareTo(Short anotherShort)
3、变为Integer
static Short valueOf(short s)
static Short valueOf(String s)
static Short valueOf(String s, int radix)
static short parseShort(String s)//十进制
static short parseShort(String s, int radix)
4、变为String
String toString()
static String toString(short s)
static Short decode(String nm)
5、变为hashCode
boolean equals(Object obj)
int hashCode()
static int hashCode(short value)
六、Float
Float底层没有缓存,
Float.valueOf((float)127.0)== Float.valueOf((float)127.0)
,返回值是fasle
1、数学计算
static float sum(float a, float b)
根据+运算符将两个 float值一起添加。
static int compare(float f1, float f2)
比较两个指定的 float值。
int compareTo(Float anotherFloat)
数字比较两个 Float对象。
static float max(float a, float b)
返回两个 float的较大值,就像调用 Math.max一样 。
static float min(float a, float b)
返回两个 float的较小值,就像调用 Math.min一样 。
2、变为Float
static Float valueOf(float f)
static Float valueOf(String s)
static int floatToIntBits(float value)//根据IEEE 754浮点“单格式”位布局返回指定浮点值的表示。
static int floatToRawIntBits(float value)//根据IEEE 754浮点“单格式”位布局返回指定浮点值的表示,保留非数字(NaN)值。
static float parseFloat(String s)//10进制
3、变为String
static String toHexString(float f)//十六进制
String toString()
static String toString(float f)
4、hashCode
在Java float比较中要使用Float.floatToIntBits(),因为NaN==NaN为fasle
boolean equals(Object obj)
int hashCode()
static int hashCode(float value)
6、数字属性判断
static boolean isFinite(float f)//如果参数是有限浮点值,则返回true ; 返回false (对于NaN和无穷大参数)。
boolean isInfinite()//如果指定的数量是无限大,返回 true
static boolean isInfinite(float v)//如果指定的数量是无限大,返回 true
boolean isNaN()//如果这个Float值是NaN,则返回 true
static boolean isNaN(float v)//如果这个Float值是NaN,则返回 true
七、Double
Double底层没有缓存,
Double.valueOf(127.0)== Double.valueOf(27.0)
,返回值是fasle
2、数学计算
static int compare(double d1, double d2)
int compareTo(Double anotherDouble)
static double max(double a, double b)
static double min(double a, double b)
static double sum(double a, double b)
static long doubleToLongBits(double value) //根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示。
static long doubleToRawLongBits(double value) //根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示,保留非数字(NaN)值。
3、变为Integer
static Double valueOf(double d)
static Double valueOf(String s)
static double parseDouble(String s)
4、变为String
static String toHexString(double d)//十六进制解析double
String toString()
static String toString(double d)
5、变为hashCode
在Java Double比较中要使用Float.doubleToLongBits(),因为NaN==NaN为fasle
boolean equals(Object obj)
int hashCode()
static int hashCode(double value)
6、数字属性判断
static boolean isFinite(double f)//如果参数是有限浮点值,则返回true ; 返回false (对于NaN和无穷大参数)。
boolean isInfinite()//如果指定的数量是无限大,返回 true
static boolean isInfinite(double v)//如果指定的数量是无限大,返回 true
boolean isNaN()//如果这个double值是NaN,则返回 true
static boolean isNaN(double v)//如果这个double值是NaN,则返回 true
八、Boolean
注意:
Boolean.valueOf(true)==Boolean.valueOf(true)
,因为Boolean把true和false对象缓存起来了
true是1
fasle是0
1、数学计算
static int compare(boolean x, boolean y)
int compareTo(Boolean b)
3、变为Boolean
static boolean getBoolean(String name)
static boolean logicalAnd(boolean a, boolean b)//&&运算符
static boolean logicalOr(boolean a, boolean b)//||运算符
static boolean logicalXor(boolean a, boolean b)//^运算符
static boolean parseBoolean(String s)
static Boolean valueOf(boolean b)
static Boolean valueOf(String s)
4、变为String
String toString()
static String toString(boolean b)
5、变为hashCode
boolean equals(Object obj)
int hashCode()
static int hashCode(boolean value)