Integer比较
/**
* @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比较的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [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 ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
随机推荐
- Eclipse扩展点实践之添加菜单项(ActionSet方式实现)
ActionSet方式比起Command方式,比较直观,但是功能有限. 首先:新建一个项目,在Extension中添加org.eclipse.ui.actionSets的扩展. 然后,new-> ...
- 碎碎念,浅饮-------Day30
这不是关于技术的文章,它偏离了我原来的计划轨迹.但,我相信这将是远远超出了技术的意义使我无论什么价格值. 高考已经开始,不知道在这片宁静的夜空下有多少人已经美美的睡了,香甜憨然.又有多少人这睡着的眼角 ...
- WPF界面设计技巧(2)—自定义漂亮的按钮样式
原文:WPF界面设计技巧(2)-自定义漂亮的按钮样式 上次做了个很酷的不规则窗体,这次我们来弄点好看的按钮出来,此次将采用纯代码来设计按钮样式,不需要 Microsoft Expression Des ...
- ReferenceError: Error #1069: 在 spark.components.RadioButtonGroup 上找不到属性 label,且没有默认值
1.错误描写叙述 ReferenceError: Error #1069: 在 spark.components.RadioButtonGroup 上找不到属性 label,且没有默认值. at Ch ...
- myeclipse中,项目上有个叉报错,文件没有错误
同事将他的java项目交接给了我.和平时的交接一样.他把他最新的源码.打成压缩包,发给我. 我解压后.使用myeclipse开发工具.通过导入,将项目导入到我的开发工具中.这个时候有一个问题出现了.在 ...
- Sql还原数据库出现3154错误
要先知道还原的数据库的名字 然后在数据库中新建一个那个名字的数据库, his 是数据库的名字!! RESTORE DATABASE his FROM DISK = 'E:\RDHL文件\HIS相关文档 ...
- ExtJs自学教程(1):从一切API开始
称号 记得 本系列文章是不是引进全套焦点ExtJs使用,您只需专注于解决ExtJs思考问题.人们不写长篇大论.别人能学会自立.l 有些人只要学会CSS的javascript对于英文不至于很蹩脚(以辅 ...
- Gradle 多渠道打包的使用和错误分析(转)
刚接触到android的开发,对什么都陌生的,本文是自己在项目中使用的技术要点总结,大咖遇到可直接飘过..... 1.Gradle 打包(不废话了直接来脚本),将下列脚本放到build.gradle文 ...
- uip UDPclient模式通信移植,p本地ort可以是无规
现在移植UDPclient模式,使用广播地址检测. //udp_client.c /********************************************************** ...
- HDOJ 5147 Sequence II 树阵
树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others) ...