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 ...
随机推荐
- 【cocos2d-x不要在生产白片步骤】第二项:制作Block分类
由于游戏非常多使用阻断,因此,我们创建了一个单独的类中Block. 于Blcok.h声明了两个初始化函数: static Block* createWithArgs(Color3B color, Si ...
- Windows 10Bash命令
Windows 10预览版14316开启Bash命令支持 00x0 前言 4月7日凌晨,微软推送了最新的Windows 10一周年更新预览版14316,其中重要的是原生支持Linux Bash命令行支 ...
- IIS设置允许下载.exe文件解决方法
最近很多客户使用IIS服务器,然后提示返现宝下载无法找到等无法下载的问题. 返现宝是.exe安装文件,部分服务器或主机可能无法下载. 第一.如果是自己服务器或VPS请按如下设置: 1.设置MIME,让 ...
- PHP草根论之设计模式-訪问者模式
关于模式本身的概念,请參考网上其他文章 此处仅仅讨论在PHP实际开发过程中的应用 此模式适用范围极为受限,适用情景: 1.适用于项目维护过程,不适用于项目开发过程 2.新增需求,要求为一个/多个类添加 ...
- wamp在win7下64位系统memcache/memcached安装教程
折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别 在自己的新程序中打算全面应用memcached ...
- Oracle练习
--声明一个变量,并给它赋值 declare v_bonus number(8); begin select id*6 into v_bonus from A where Id=5; DBMS_OU ...
- Web测试基于实际测试的功能测试点总结--转载
文章来源:http://www.51testing.com/html/99/n-854599.html 好文章就该记录一下\(^o^)/~ 一.页面链接检查:测试每一个链接是否都有对应的页面,并且页面 ...
- 怎么样excel其产生的条形码(10分钟的时间excel)从而出现了条形码
现在快递行业.京东购物,这样一来,使用条码管理,因此,如何在你的excel其中还生产商品条码管理它?其实很easy,4步骤学会!10分钟搞定. 1.从网址如下.下载字体, 2.双击安装字体. 3,在e ...
- Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)
集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...
- 深度分析DataTable如何筛选
这项DataTable指SpryMedia专为HTML Table设计的插件页面和发展,官方网站:http://legacy.datatables.net/index. 左右oSettings.aiD ...