Integer类型
四字节,32位长度;
最大值为2的31次方结果减1;最小值为负的2的31次方;
Integer对象的equals方法内部实际使用【==】进行两个对象的数值比较。
Integer对象的hashcode就是Integer类型的数值;
Integer对象的compareTo方法,比较的是两个Integer对象值的大小;
public static void main(String[] args) {
Integer integer1 = 127;
Integer integer2 = 127;
Integer integer3 = new Integer(127);
Integer integer4 = new Integer(127);
System.out.println(integer1 == integer2);
System.out.println(integer1 == integer3);
System.out.println(integer4 == integer3);
Integer integer5 = 128;
Integer integer6 = 128;
Integer integer7 = new Integer(128);
Integer integer8 = new Integer(128);
System.out.println(integer5 == integer6);
System.out.println(integer7 == integer8);
System.out.println(integer5 == integer8);
}
输出结果:
true
false
false
false
false
false
Integer内部有一个缓存类,IntegerCache,它设计默认把从-128到127的Integer对象缓存,那么直接使用数值在这个范围的非new出来的Integer对象,其实使用的就是同一地址下的对象,所以就认为是相等的。
Integer中常用的parseInt方法,用来把String类型转成指定进制下的Integer类型:
- 进制要求在不能小于2,不能大于36;
- String类型要求第一位的ASCII码只能为【+】【-】和【数字】,其他情况会抛出转换异常;
- String类型的非第一位不是【数字】也会抛出转换异常;
Long类型
8字节,64位长度;
最大值为2的63次方结果减一,最小值为负的2的63次方;
Long类型的equals方法,比较的是两个Long类型对象的数值是否相等。
Long类型的hashcode值,是当前Long对象的数值向右先进行32位的移动,然后用结果与原Long对象进行求异或得出的结果;
Long类型的compareTo方法,是将两个Long对象的数值进行大小比较。
Long类型内部也有一个缓存类,LongCache,它将-128到127通过new Long()的方式创建出256个对象,放在数组中,与Integer实现方式类似。
public static void testLong(){
Long a =127l;
Long b =127l;
System.out.println(a==b);
Long a1 = 128L;
Long b1 = 128L;
System.out.println(a1 == b1);
}
true
false
Long类型常用的parseLong方法,用来把String类型转成指定进制下的数字类型。
- 进制要求在不能小于2,不能大于36;
- String类型要求第一位的ASCII码只能为【+】【-】和【数字】,其他情况会抛出转换异常;
- String类型的非第一位不是【数字】也会抛出转换异常;
Short类型
2个字节,16位;
最大值为2的15次方结果减一;最小值为负的2的15次方;
Short类型的equals方法,比较的是两个Short类型对象的数值是否相等。
Short类型的hashcode方法,得到的是把short值强制转成int值进行返回 。
Short类型的compareTo方法,是将两个Short对象的数值进行大小比较。
Short类型内部,也有一个缓存类,ShortCache,管理了-128至127。
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类型的parseShort方法,内部先会使用Integer.parseInt()方法得到一个int值结果。在对该结果进行不能小于最小值和不能大于最大值的判断后,将结果转成short类型返回。
Byte类型
1个字节,8位;
最大值为2的7次方结果减一,也就是127;最小值是负的 2的7次方,也就是-128;
Byte类型里有个ByteCache类,内部缓存了Byte类型的-128到127,也就是直接把整个Byte类型范围内的所有数值缓存了。
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类型的equals方法,比较的是两个Byte类型的数值是否【==】。
Byte类型的hashcode方法,得到的结果是将byte值转成int类型后返回。
Byte类型的compareTo方法,通过让两个对象相减得到比较结果。
Byte类型的parseByte方法,内部先会使用Integer.parseInt()方法得到一个int值结果。在对该结果进行不能小于最小值和不能大于最大值的判断后,将结果转成short类型返回。
Float类型
32位;
会有精度丢失的情况。
Float的hashcode结果,是调用本地native方法floatToRawIntBits进行计算得到的hashcode值;
Float的equals方法,是两个Float对象的hashcode值进行比较是否【==】;
Float的compareTo方法,也是两个Float对象的hashcode值进行比较的。
Float的parseFloat默认转成10进制的。会校验字符串的正负符号、小数点、数字等信息。
Double类型
64位:
会有精度丢失的情况;
Double的hashcode结果,是调用本地native方法doubleToRawLongBits进行计算得到的hashcode值;
Double的equals方法,是两个Float对象的hashcode值进行比较是否【==】;
Double的compareTo方法,也是两个Float对象的hashcode值进行比较的。
Doubel的parseDouble默认转成10进制的。会校验字符串的正负符号、小数点、数字等信息。底层实现与Float方法的底层实现是同一个方法,在返回值的时候一个选择了doubleValue,一个选择了floatValue。
Boolean类型
True / False;
Boolean的equals方法,用【==】比较两个Boolean对象的值是否一致;
Boolean的hashcode方法,当前为true返回1231,为false返回1237;
Boolean的compareTo方法,比较两个Boolean对象的值是否一致,不一致时哪个为true就返回哪个。
Boolean类型里的parseBoolean方法,比较字符串和字符串true是否相等。
Character类型
可看作是char类型的包装类。
Character类型的equals方法,比较两个Character对象的char值是否相等【==】。
Character类型的hashcode方法,返回当前char值的ASCII码的int值。
Character类型的compareTo方法,两个对象的char值进行相减。
Character类型内部有个缓存类CharacterCache,将所有ASCII码进行了缓存。
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
具体ASCII的码值可查看百度百科