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参数,这里需要对参数 ...
随机推荐
- C# DataTable删除行Delete与Remove的问题
DataTable删除行使用Delete后,只是该行被标记为deleted,但是还存在,用Rows.Count来获取行数时,还是删除之前的行数,需要使用datatable.AcceptChanges( ...
- SEC7 - MySQL 查询语句--------------进阶3:排序查询
# 进阶3:排序查询 /* 引入: select * from employees; 语法: select 查询列表 from 表 [where 筛选条件] order by 排序的列表 asc/de ...
- Ubuntu 18.04安装docker 以及Nginx服务设置
1.安装需要的包sudo apt install apt-transport-https ca-certificates software-properties-common curl 2.添加 GP ...
- java绘制带姓的圆
public class ImageGenerator { private static final Color[] colors = new Color[] { new Color(129, 198 ...
- C语言接口
struct i_foo * foobar_foo(void); //返回接口指针struct foo_object * foo_create(struct i_foo *iface, void *d ...
- Django学习——开发你的第一个Django应用1
突然对Django热情似火,所以就开学习了,我是根据官方文档学习的,所以我打算把官方文档翻译一遍,全当学习,首先贴官方文档的地址:https://docs.djangoproject.com/en/1 ...
- 关于GeneXus中的ForeachCommand命令
首先作为我们开发过程中必不可少的命令For Each 有着无与伦比的重要性 但是我们从Wiki上得知 我们用到的可能只是它一丢丢的能力并没有全部使用出来. 所以 这篇文档将记 ...
- Redis线上环境做Keys匹配操作!你可以离职了!
转自:https://blog.csdn.net/bntx2jsqfehy7/article/details/84207884一.一个新闻 新闻内容如下:php工程师执行redis keys * 导致 ...
- Git--08 Jenkins
目录 Jenkins 01. 安装准备 02 .安装Jdk和Jenkins 03 .配置Jenkins 04. 插件安装 05. 创建项目 06. Jenkins获取Git源代码 07. 立即构建获取 ...
- openssl版本升级
1.查看openssl版本 [root@node2 openssh-8.0p1]# openssl version OpenSSL 1.0.1e-fips 11 Feb 2013 2.下载指定版本的o ...