Integer类分析(jdk8)
一、构造函数
1. Integer类继承Number类,实现Comparable接口,重写了compareTo()方法。
2. Integer最小值为-2147483648,最大值为2147483647,若数值超出这个范围会报错。
3. Integer变量的声明可以通过Integer i = new Integer(100)声明;也可以通过Integer i = 100声明,其实调用的是Intager.valueOf(100)方法。
4.compareTo()方法的实现是: 如果x>y,则返回-1;如果x==y,则返回0;如果x<y,则返回1。
public final class Integer extends Number implements Comparable<Integer> {
@Native public static final int MIN_VALUE = 0x80000000; // -2147483648
@Native public static final int MAX_VALUE = 0x7fffffff; // 2147483647
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
private final int value;
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}
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);
}
}
二、Integer缓存
1. Integer把[-128,127]的数字放入缓存,以下面代码为例
当使用equals()方法时,无论数字是几,都是true。
当使用new Integer(num)声明一个变量时, 用==比较永远是false,因为new申请的是两块不同的内存空间,==比较的是内存地址,所以肯定不同。
当使用直接赋值声明一个变量时,若变量范围在[-128,127]时,==比较为true,因为它们用的是同一块缓存;超出的话会重新new一个Ingeter()变量,地址就不同了,就是false。
Integer integer1 = new Integer(128);
Integer integer2 = new Integer(128);
Integer a = 128;
Integer b = 128;
System.out.println(integer1 == integer2); // -129:false -128:false 127:false 128:false
System.out.println(integer1.equals(integer2)); // -129:true -128:true 127:true 128:true
System.out.println(a == b); // -129:false -128:true 127:true 128:false
System.out.println(a.equals(b)); // -129:true -128:true 127:true 128 true
2. * Integer (-128~127)
* Boolean (all) * Byte (all) * Character (<=127) * Short (-128~127) * Long (-128~127) * Float (none) * Double (none)private static class IntegerCache {
static final int low = -128; // -128
static final int high; // 127
static final Integer cache[];
static {
// high value may be configured by property
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);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
三、valueOf()
给变量直接赋值的时候(Integer i = 100),就是使用valyeOf()方法来实现的。
由源码可以看到,若i在缓存范围内,则返回缓存中的数值,否则的话返回一个新的变量。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
四、equals()
把比较的变量转换为int进行比较
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
五、hashCode()
可以看到这里的hashCode就是value本身
@Override
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
六、comareTo()
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);
}
Integer类分析(jdk8)的更多相关文章
- JDK源码之Integer类分析
一 简介 Integer是int基本类型的包装类,同样继承了Number类,实现了Comparable接口,String类中的一些转化方法就使用了Integer类中的一些API,且fianl修饰不可继 ...
- Java Integer类分析
public static final int MIN_VALUE = 0x80000000; -2^31 public static final int MAX_VALUE = 0x7ff ...
- 【Java EE 学习 69 下】【数据采集系统第一天】【实体类分析和Base类书写】
之前SSH框架已经搭建完毕,现在进行实体类的分析和Base类的书写.Base类是抽象类,专门用于继承. 一.实体类关系分析 既然是数据采集系统,首先调查实体(Survey)是一定要有的,一个调查有多个 ...
- Java—Integer类
Integer类 Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 该类提供了多个方法,能在 int 类型和 String 类型之间 ...
- Java中Integer类的方法
java.lang 类 Integer java.lang.Object java.lang.Number java.lang.Integer 全部已实现的接口: Serializable, Comp ...
- 浅谈 Integer 类
在讲解 Integer 之前,我们先看下面这段代码: public static void main(String[] args) { Integer i = 10; Integer j = 10; ...
- 13-03 Java 基本类型包装类概述,Integer类,Character
基本类型包装类概述 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据.常用的操作之一:用于基本数据类型与字符串之间的转换.基本类型和包装类的对应Byte,Short,Inte ...
- Integer 类和 int 的区别
http://www.cnblogs.com/ysocean/p/8075676.html public static void main(String[] args) { Integer i ...
- Integer代码分析
我们都知道Integer是int的封装类,提供了一些类型转换等工具方法,有一个-128-127的缓存,而且是final的. ----------------------------- 干货: Inte ...
随机推荐
- mongo distinct 指定条件
db.Article.distinct("字段名称",{"Comment.Reply.email" : "xxx"})
- BCB直接访问硬件端口和物理内存 - WinIO的应用
BCB直接访问硬件端口和物理内存 - WinIO的应用 (读硬盘参数和主板BIOS信息, 支持 Win9x/NT/2k/XP/2003) 关于直接访问端口, 有很多网站很多文章都讨论过, 但总找不到非 ...
- java 获取request参数集
request里有两个方法 request.getParameterMap(); request.getParameterNames(); 我想用这两种方法获取. 1.用request.getPara ...
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
- DataNode启动不成功——java.net.BindException: Port in use: localhost:0 Caused by: java.net.BindException: Cannot assign requested address解决办法
爱折腾的人总是会出线各种奇怪的问题.记得之前听一位大师讲过,我们不能踩完前进路上的所有坑前进,而应该学会怎样避开前进路上的坑,踩得坑越多,可能你的经验越丰富,但是付出的时间代价可能不是经验能换来的.我 ...
- Firebug入门指南(转)
本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...
- gh-ost原理
gh-ost原理 一.三种模式架构图 1.连上从库,在主库上修改 这是gh-ost默认的工作模式,它会查看从库情况,找到集群的主库并且连接上去,对主库侵入最少,大体步骤是: 在主库上创建_xxx_gh ...
- 单元测试框架unittest,ddt
unittest case.py 测试用例 suite.py 测试套件 loader.py 加载测试用例 run.py 执行测试用例 result.py 测试结果,测试报告 main mock 模拟测 ...
- centos7 -lvm卷组
老忘,记一下 基本的逻辑卷管理概念: PV(Physical Volume)- 物理卷 物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备. ...
- Graph_Master(连通分量_A_双连通分量+桥)
hdu 5409 题目大意:给出一张简单图,求对应输入的m条边,第i-th条边被删除后,哪两个点不连通(u,v,u<v),若有多解,使得u尽量大的同时v尽量小. 解题过程:拿到题面的第一反应缩点 ...