/**
* @time 2014-06-25
* @author Cao HaiCheng
*
*/
public class demo {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}
/**
* 第一个答案是false非常好理解,由于'=='操作符比較的是两个对象的地址,a和b指向的地址不同
*/
private static void test1() {
Integer a = new Integer(50);
Integer b = 50;
System.out.println("test1执行结果:"+(a == b)); //false
} /**
* 这个答案是true,Integer a=50属于自己主动装箱,调用的是编译器中的public static Integer valueOf(int i)方法
* 我们看下这种方法:
* public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
} *
* 我们能够看到jdk源代码中定义的这种方法意思是这种:当i的值在某个范围之间的时候不用创建对象,直接去IntegerCache中取,再看下这个
* IntegerCache类:
* 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");
if (integerCacheHighPropValue != null) {
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);
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
} private IntegerCache() {}
}
* 我们看到这个Cache里面放了256个值,就是-128到127之间的值
* 所以当Integer a = 50; 的时候并没有创建新的对象,还是引用的缓存池中的地址,所以这个结果为true
*/
private static void test2() {
Integer a = 50;
Integer b = 50;
System.out.println("test2执行结果:"+(a == b)); //true
} /**
* 这个依据上面那个说法就简单了,由于150并不在-128到127之间,所以这个须要自己创建对象,创建的对象a和b的指向地址不同
* 所以该结果为false;
*/
private static void test3() {
Integer a = 150;
Integer b = 150;
System.out.println("test3执行结果:"+(a == b));//false
} /**
* 这个 Integer a = Integer.valueOf(50); 和Integer b = 50; 调用的方法都是编译器中的public static Integer valueOf(int i)方法
* 所以两个50都没有创建新的对象,都是从缓存池中拿到的对象,所以结果为true
*/
private static void test4() {
Integer a = Integer.valueOf(50);
Integer b = 50;
System.out.println("test4执行结果:"+(a == b)); //true
} /**
* 同理,数值超出了范围,所以指向不同,结果为false
*/
private static void test5() {
Integer a = Integer.valueOf(150);
Integer b = 150;
System.out.println("test5执行结果:"+(a == b)); //false
} }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Integer比较的更多相关文章

  1. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  10. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

随机推荐

  1. Linux scp文件复制

    scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.  1.scp命令的用处:  scp在网络上不同的主机之间复制文件,它使用ssh安全协议传输 ...

  2. CSDN开源夏令营 百度数据可视化实践 ECharts(8)问题分析

    ECharts问题描写叙述: 问题就是折线图上的点是显示的,有人问能不能一開始不显示,当你点击的时候或者是当鼠标移动到上面的时候,折线上的点才显示? 例如以下图所看到的: 分析:让折线上的点不显示能够 ...

  3. Failed to load libGL.so问题解决

    Ubuntu 14.04下启动模拟设备Android 4.2.2的时候报错: failed to load libgl.so 先用locate 命令定位libGL库, 然后加入�一个链接就可以: de ...

  4. [Sqlite] 移动嵌入式数据库Sqlite日报SQL操作语句汇总

    ,EXPLAIN分析 没有建立索引之前.分析都是表扫描: sqlite> EXPLAIN SELECT *  FROM COMPANY  WHERE Salary < 20000; add ...

  5. HDU 3315 My Brute(费用流)

    职务地址:HDU 3315 这个题的思路全然是自己想出来的,自我感觉挺巧妙的. . .(大牛勿喷.. . )对大胆建图又多了一份信心. 详细思路是构造一个二分图,Si连源点.Xi连汇点,流量都是1,费 ...

  6. Java-UrlRewrite中文api文档

    安装 1. 下载jar包, 并加入到WEB-INF/lib下 2. 在WEB-INF/web.xml中增加下面的配置 <filter> <filter-name>UrlRewr ...

  7. [置顶] JUnit入门教程(二)

    一:介绍 接着上次的课程,今天我们学习JUnit4的新特性 assertThat()方法,这种方式和其余的assert方法比起来,更加接进英语. 二:分析API API中的例子: 参数 T Actua ...

  8. Kruskal(克鲁斯卡尔)

    设有一个有n个顶点的连通网N={V,E},最初先构造一个只有n个顶点, 没有边的非 连通图 T={V, E}, 图中每个顶点自成一个连通分量. 当在E中选到一条具有最小权值的边时,若该边的两个顶点落在 ...

  9. 编C语言单元测试框架CUnit方法库

    /*********************************************************************  * Author  : Samson  * Date   ...

  10. linux下查找某个文件

    参考http://blog.csdn.net/gray13/article/details/6365654 一.通过文件名查找法: 举例说明,假设你忘记了httpd.conf这个文件在系统的哪个目录 ...