java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项
先看一下下面的结果
1.System.out.println(127==127); //true , int type compare
2.System.out.println(128==128); //true , int type compare
3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare
4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare
5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object
6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache
7.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true , int type compare
解释
int整型常量比较时,== 是值比较,所以1,2返回true。1,2是值比较。
new Integer() 每次构造一个新的Integer对象,所以3返回false。3是对象比较。
Integer.parseInt每次构造一个int常量,所以4返回true。4是值比较。
Integer.valueOf返回一个Integer对象,默认在-128~127之间时返回缓存中的已有对象(如果存在的话),所以5返回true,6返回false。5,6是对象比较。
第7个比较特殊,是int 和 Integer之间的比较,结果是值比较,返回true。
总结
对于整型的比较,首先判断是值比较还是对象比较,值比较肯定返回true,有一个是值就是值比较。对象比较,则看对象是怎么构造出来的,如果是采用new Integer方式,则每次产生新对象,两个new出来的Integer比较肯定返回false,如果是Integer.valueOf方式的话,注意值的区间是否在-128~127之间,如果在,则构造的相同值的对象是同一个对象,==比较后返回true,否则返回false。
所以,对于值比较==放心没问题,对于Integer的比较最好用equals方法比较对象内容,当然注意先判断Integer是否不为null。
知识扩展
针对Integer.valueOf源码分析一下
1.我们调用的Integer.valueOf方法, 它先调用parseInt转成int型数值,再调它自己的重载方法
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
2.Integer.valueOf重载方法,根据数值i的大小,决定是否从缓存中取一个Integer对象
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
3.Integer的构造函数,非常简单
public Integer(int value) {
this.value = value;
}
4.IntegerCache静态类,是Integer的内部类,三个属性(一个缓存的Integer型数组+一组缓存范围)
private static class IntegerCache {
static final int low = -128;
static final int high;
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"); //读取VM参数
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue); //配置值转换成int数值
i = Math.max(i, 127); //和127比较,取较大者
// Maximum array size is Integer.MAX_VALUE(控制缓存数组的大小,最大为整型的最大值,这样一来,h值就必须小于整型最大值,因为要存 -128~0这129个数嘛)
h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //实际就是 h=Math.min(i,Integer.MAX_VALUE-129),正整数能缓存的个数
} 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++); //将一些int常量缓存进Integer对象数组缓存中去
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127; //如果小于127,抛异常
}
private IntegerCache() {}
}
看完上述Integer.valueOf源码后,你就会发现,默认的Integer缓存int常量池是可以配置的,配置方法是添加VM参数,加: -Djava.lang.Integer.IntegerCache.high=200
Intellij IDEA 运行配置的VM Options选项中添加参数即可。
java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项的更多相关文章
- Java中Integer.parseInt和Integer.valueOf,你还傻傻分不清吗?
在Java的Integer类中,有Integer.valueOf(String s)和Integer.parseInt(String s)两个静态方法,他们都能够将字符串转换为整型,他们到底有什么区别 ...
- Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...
- Integer.valueOf与Integer.parseInt的小疑惑
参考博客: http://www.importnew.com/9162.html 测试代码如下: public class Main { /** * 备注:结果跟你的JDK版本有关系: * * 我的是 ...
- Integer.parseInt()和Integer.valueOf()方法详解
1.Integer.parseInt(): public static int parseInt(String s) throws NumberFormatException { return par ...
- Java 源码学习系列(三)——Integer
Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还 ...
- Java面试必看之Integer.parseInt()与Integer.valueOf()
Integer.parseInt()和Integer.valueOf()都是将成为String转换为Int,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深挖 ...
- 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()
Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深 ...
- Integer.parseInt()和Integer.valueOf()有什么区别
jdk的源代码的时候注意到Integer.parseInt(s) 和 Integer.valueOf(s)的具体代码的实现有所区别: Java代码 public static int parseInt ...
- JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果
先来看段代码 public class IntegerDemo { public static void main(String[] args) { String num = null; System ...
随机推荐
- Multi account chang login with multi -thread
void worker_DoWork(object sender, DoWorkEventArgs e) { isBussy = true; if (Common.isChangingAccount) ...
- 使用HibernateDaoSupport抽取BaseDao
在开发采用Struts2+Spring+hibernate这三大框架的项目时,我们需要一个抽取一个BaseDao.这个Dao里面CRUD都给封装好,后续的其他Dao直接用它的功能就可以 ...
- python 单体模式 的几种实现
这是本人的一篇学习笔记. 本文用 python 实现单体模式,参考了这里 一.修改父类的 __dict__ class Borg: _shared_state = {} def __init__(se ...
- Windows下的Anaconda+OpenCV的环境配置
Windows下的Anaconda+OpenCV的环境配置
- 【HNOI2016】序列
题面 题解 设\([l, r]\)的最小值的位置为\(p\),那么对于左端点在区间\([l, p]\),右端点在区间\([p, r]\)的区间最小值都为\(a[p]\). 这一部分的贡献就是\(a[p ...
- 详解C#7.0新特性
1. out 变量(out variables) 以前我们使用out变量必须在使用前进行声明,C# 7.0 给我们提供了一种更简洁的语法 “使用时进行内联声明” .如下所示: 1 var input ...
- Markdown语言学习
看够了单一的文本文档么?或者写一个word各种调整样式?试试Markdown吧! Markdown是一种文本标记语言,通过简单的标记语法,使单一的文本内容具有一定的格式. 下面来看看常用的各种标记吧 ...
- 《杜增强讲Unity之Tanks坦克大战》1-准备工作
0.案例介绍 0.1开始界面 点击Play Now 进入游戏界面 左边的坦克使用ws控制前后移动,ad键左右旋转,空格键开火 右边的坦克使用方向键上下控制前后移动,方向键左右键实现左右旋转 ...
- 20170929php
这是之前学习PHP类使用的代码 <?phpclass animal{ var $name="1"; var $sex="2"; public static ...
- 冲刺Two之站立会议8
今天对软件进行了用户试用,找了一些同学让他们试用软件之后对软件给出了建议,这样我们可以在一定程度上对它进行进一步地优化.