Jdk1.8 之 Integer类源码浅析
先看一下它的继承、实现关系:
public final class Integer extends Number implements Comparable<Integer>
Number是个抽象类,大概包含六个抽象方法,都是用来类型转换的 具体代码如下:
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -8742448824652078965L;
}
这就是继承后 子类的实现,强转。
public long longValue(http://www.amjmh.com) {
return (long)value;
}
看下Comparable 接口,只有一个compareTo 比较大小方法:
public int compareTo(T o);
看下Integer的具体
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
如果看代码有点费劲,那复制如下代码,自己运行下看结果就懂了。
public class Test {
public static void main(String[] args) {
Integer it1=128;
Integer it2=-128;
Integer it3=128;
System.out.println(it2.compareTo(it1)+"-----x < y");
System.out.println(it1.compareTo(it2)+"------x > y");
System.out.println(Integer.compare(it1,it3)+"-----x == y");
}
}
打印结果:
-1-----x < y
1------x > y
0-----x == y
2. Integer的缓存问题
Integer默认放入缓存的区间是-128 至127 。但最大緩存值可以手動調整,請看:
The Javadoc comment clearly states that this class is for cache and to support the autoboxing of values between 128 and 127. The high value of 127 can be modified by using a VM argument -XX:AutoBoxCacheMax=size. So the caching happens in the for-loop. It just runs from the low to high and creates as many Integer instances and stores in an Integer array named cache. As simple as that. This caching is doing at the first usage of the Integer class. Henceforth, these cached instances are used instead of creating a new instance (during autoboxing).
運行以下代碼看效果:
public class IntegerTest {
public static void main(String[] args) {
Integer it1=2008;
Integer it2=2008;
System.out.println(it1==it2);//true 说明:为啥超了127了还是true呢?因为我设置了vm arguments参数 -XX:AutoBoxCacheMax=2008
Integer it1t=2009;
Integer it2t=2009;
System.out.println(it1t==it2t);//false 说明:超过 2008 false了吧~~
Integer newit1=new Integer(127);
Integer newit2=new Integer(127);
System.out.println(newit1==newit2);//false 说明:New的话是在heap上占用独立内存的新对象
int it3=127;
int it4=127;
int it5=2008;
System.out.println(it3==it4);//true
System.out.println(it1==it5);//true
}
}
print result :
true
false
false
true
true
設置虛擬機啓動參數
---------------------
Jdk1.8 之 Integer类源码浅析的更多相关文章
- Integer类源码浅析
1.首先Integer提供了两类工具类,包括把一个int类型转成二进等, 其实执行转换算法只有一个方法: public static String toString(int i, int radix) ...
- Long类源码浅析
1.Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的: 关于Integer类的浅析可以参看:Integer类源码浅析 2.这里主要介绍一下LongCache类,该缓存 ...
- [原创]Android系统中常用JAVA类源码浅析之HashMap
由于是浅析,所以我只分析常用的接口,注意是Android系统中的JAVA类,可能和JDK的源码有区别. 首先从构造函数开始, /** * Min capacity (other than zero) ...
- ArrayList类源码浅析(一)
1.首先来看一下ArrayList类中的字段 可以看出,ArrayList维护了一个Object数组,默认容量是10,size记录数组的长度: 2.ArrayList提供了三个构造器:ArrayLis ...
- java.lang.Byte 类源码浅析
Byte 类字节,属于Number. public final class Byte extends Number implements Comparable<Byte> { /** * ...
- LinkedList类源码浅析(一)
1.先来看一看LinkedList类的字段和构造方法 size记录链表的长度,first永远指向链表的第一个元素,last永远指向链表的最后一个元素 提供两个构造方法,一个无参的构造方法,一个接受一个 ...
- ArrayList类源码浅析(三)
1.看一个示例 运行上述代码,抛出一个异常: 这是一个典型的并发修改异常,如果把上述代码中的125行注释,把126行打开,运行就能通过了: 原因: 1)因为在迭代的时候,使用的是Itr类的对象,在调用 ...
- ArrayList类源码浅析(二)
1.removeAll(Collection<?> c)和retainAll(Collection<?> c)方法 第一个是从list中删除指定的匹配的集合元素,第二个方法是用 ...
- LinkedList类源码浅析(二)
1.上一节介绍了LinkedList的几个基本的方法,其他方法类似,就不一一介绍: 现在再来看一个删除的方法:remove(Object o) remove方法接受一个Object参数,这里需要对参数 ...
随机推荐
- HDU 1269 迷宫城堡 (Kosaraju)
题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...
- Manacher(最长镜面回文串)
I - O'My! Gym - 101350I Note: this is a harder version of Mirrored string I. The gorillas have recen ...
- kmp(多次可重叠匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1686 Oulipo Problem Description The French author Georges ...
- dp(最长公共上升子序列)
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...
- springcloud费话之Eureka接口调用(feign)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- 2018-11-3-git-分支改名
title author date CreateTime categories git 分支改名 lindexi 2018-11-3 12:49:9 +0800 2018-2-13 17:23:3 + ...
- liunx-网络基础
liunx 网络配置 ifconfig: ipconfig -a ;显示信息 ifconfig eth1 up //开启网络接口 ifconfig eth1 down //关闭网络接口 ifconf ...
- poland 波兰 时区
http://www.timeofdate.com/country/Poland 2019年 ~ 2020年波兰夏令时开始结束时间 年份 日期 类型 2019 2019-3-31 夏令时开始 20 ...
- Linux双网卡绑定bond详解
参考资料: 1.https://blog.csdn.net/shengerjianku/article/details/79221886
- 【python实例】判断是否是回文数
""" 输入一个数,判断一个这个数是否是回文数.例如:121,这个数反过来还是121,所以这个是回文数: 再如:134,这个数反过来是431,所以这不是一个回文数: 12 ...