/**
* @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. BGP的状态机制

    Idle 状态:即空闲状态,不接受任何BGP的连接,等待Start事件的产生,如果有start事件产生,若有start事件产生,系统开启ConnectRetry定时器,向邻居发起TCP连接,并将状态变 ...

  2. C:打印菱形(自己的方法)

    //-------------------*打印菱形*--------------------- int i,j,k; int n; printf("请输入一个奇数n:"); sc ...

  3. Codeforces 4A-Watermelon(意甲冠军)

    A. Watermelon time limit per test 1 second memory limit per test 64 megabytes input standard input o ...

  4. [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9613   Accepted: 2 ...

  5. 《循序渐进Oracle》部分笔记

    1.不要用户名/密码 直接/as sysdba 是操作系统认证方式,改变安全方式 sqlnet.ora 里SQLNET.AUTHENTICATION_SERVICES=(NTS)表示启动操作系统认证; ...

  6. loading加载中效果

    (function(){ try{ var ui={ loading:{ addCssStyle:function(text) { var head = document.getElementsByT ...

  7. windows phone (19) 深入了解TextBlock

    原文:windows phone (19) 深入了解TextBlock TextBlock 一般用于显示文本的元素,我们最为经常用到的是该类的Text属性,其实显示文本有两种呈现方式,一个是设置内部文 ...

  8. Error: ORA-16501: the Data Guard broker operation failed ORA-16625: cannot reach database

    在备库上建配置文件 DGMGRL> create configuration 'sharkdbbork' as primary database is 'sharkdb' connect ide ...

  9. C陷阱与缺陷之语法陷阱

    2.1理解函数声明 不论什么C变量的声明都由两部分组成:类型以及一组类似表达式的声明符号.比如 float f; 这个声明的含义是:当对其求值时,表达式f和g的类型为浮点数类.由于声 明符与表达式的相 ...

  10. 使用javascript开发2048

    嗯,团队队友开发了一个简单的2048...哈哈,没办法,这游戏那么疯狂,必须搞搞啦,大家能够直接粘贴代码到一个html文件,直接执行就可以 依赖文件:jquery,假设乜有,大家能够自己下载一份 &l ...