【转】关于Integer大小比较的问题
昨天跟朋友一起吃饭,他说到两个Integer类型值比较是否相等的问题。经理也在这里把java的Integer值比较的问题拿出来说一说。因为在面试的时候这个是经常遇到的问题。
首先给大家看一个例子:
1![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Test
{
2![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
4
* 关于Integer大小比较问题
5
* @author 经理
6
*/
7![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
8
Integer a = 10;
9
Integer b = 10;
10
System.out.println("a==b : " + String.valueOf(a==b));
11
System.out.println("a.equals(b) : " + String.valueOf(a.equals(b)));
12
}
13
}
14![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
运行一下,在控制台得到的结果是这样的
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
当是当我们变换一下值
1![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Test
{
2![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
4
* 关于Integer大小比较问题
5
* @author 经理
6
*/
7![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
8
Integer a = 1000;
9
Integer b = 1000;
10
System.out.println("a==b : " + String.valueOf(a==b));
11
System.out.println("a.equals(b) : " + String.valueOf(a.equals(b)));
12
}
13
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
运行一下,控制台的结果又是
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
这是什么原因呢?我当时也有点犯糊涂。有点冲动就直接开源码看了。
实际上在我们用Integer a = 数字;来赋值的时候Integer这个类是调用的public static Integer valueOf(int i)这个方法。
1![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static Integer valueOf(int i)
{
2
if(i >= -128 && i <= IntegerCache.high)
3
return IntegerCache.cache[i + 128];
4
else
5
return new Integer(i);
6
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.blogjava.net/Images/dot.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们来看看ValueOf(int i)的代码,可以发现他对传入参数i做了一个if判断。在-128<=i<=127的时候是直接用的int原始数据类型,而超出了这个范围则是new了一个对象。我们知道"=="符号在比较对象的时候是比较的内存地址,而对于原始数据类型是直接比对的数据值。那么这个问题就解决了。
至于为什么用int型的时候值会在-128<=i<=127范围呢呢?我们知道八位二进制的表示的范围正好就是-128到127。大概就是因为这吧。
把平时出现的问题记录下来,自己就可以一点一点进步了。