本文共 11403 字,大约阅读时间需要 38 分钟。
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 ,这可能涉及舍入或截断。
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,因为他们没有到缓存中取值
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返回1static int compareUnsigned(int x, int y) //忽略符号比较int compareTo(Integer anotherInteger)//如果当前值大于anotherInteger返回1
static Integer valueOf(int i)static Integer valueOf(String s)//s不能为nullstatic 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是以几进制解析
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)
int hashCode()//底层就是返回Integer的valuestatic int hashCode(int value)
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 值的二进制补码表示形式而得到的值。
static Integer getInteger(String nm) 确定具有指定名称的系统属性的整数值。static Integer getInteger(String nm, int val)确定具有指定名称的系统属性的整数值。static Integer getInteger(String nm, Integer val)返回具有指定名称的系统属性的整数值。
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);}
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大返回1int compareTo(Long anotherLong) //当前值大返回 1static int compareUnsigned(long x, long y) //忽略符号比较//返回dividend/divisor的无符号商static long divideUnsigned(long dividend, long divisor)//返回dividend%divisor的无符号余数static long remainderUnsigned(long dividend, long divisor)
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是进制,非负数解析
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)//指定进制解析非负数,如果是负数报错
boolean equals(Object obj)//比较的是value值int hashCode()//hashCode是value值static int hashCode(long value)
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 值的二进制补码表示形式而得到的值。
static Long getLong(String nm)//确定 long具有指定名称的系统属性的值。static Long getLong(String nm, long val)//确定 long具有指定名称的系统属性的值。static Long getLong(String nm, Long val)//以指定的 long返回系统属性的 long值。
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];}
static int compare(byte x, byte y)int compareTo(Byte anotherByte)
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)
String toString()static String toString(byte b)
boolean equals(Object obj)int hashCode()static int hashCode(byte value)
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); }
static int compare(short x, short y)int compareTo(Short anotherShort)
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)
String toString()static String toString(short s)static Short decode(String nm)
boolean equals(Object obj)int hashCode()static int hashCode(short value)
Float底层没有缓存,
Float.valueOf((float)127.0)== Float.valueOf((float)127.0)
,返回值是fasle
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一样 。
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进制
static String toHexString(float f)//十六进制String toString()static String toString(float f)
在Java float比较中要使用Float.floatToIntBits(),因为NaN==NaN为fasle
boolean equals(Object obj)int hashCode()static int hashCode(float value)
static boolean isFinite(float f)//如果参数是有限浮点值,则返回true ; 返回false (对于NaN和无穷大参数)。boolean isInfinite()//如果指定的数量是无限大,返回 truestatic boolean isInfinite(float v)//如果指定的数量是无限大,返回 trueboolean isNaN()//如果这个Float值是NaN,则返回 true static boolean isNaN(float v)//如果这个Float值是NaN,则返回 true
Double底层没有缓存,
Double.valueOf(127.0)== Double.valueOf(27.0)
,返回值是fasle
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)值。
static Double valueOf(double d)static Double valueOf(String s)static double parseDouble(String s)
static String toHexString(double d)//十六进制解析doubleString toString()static String toString(double d)
在Java Double比较中要使用Float.doubleToLongBits(),因为NaN==NaN为fasle
boolean equals(Object obj)int hashCode()static int hashCode(double value)
static boolean isFinite(double f)//如果参数是有限浮点值,则返回true ; 返回false (对于NaN和无穷大参数)。boolean isInfinite()//如果指定的数量是无限大,返回 truestatic boolean isInfinite(double v)//如果指定的数量是无限大,返回 trueboolean isNaN()//如果这个double值是NaN,则返回 true static boolean isNaN(double v)//如果这个double值是NaN,则返回 true
注意:
true是1 fasle是0Boolean.valueOf(true)==Boolean.valueOf(true)
,因为Boolean把true和false对象缓存起来了
static int compare(boolean x, boolean y)int compareTo(Boolean b)
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)
String toString()static String toString(boolean b)
boolean equals(Object obj)int hashCode()static int hashCode(boolean value)
转载地址:http://civkk.baihongyu.com/