Java判断Integer类型的值是否相等
我们知道Integer是int的包装类,在jdk1.5以上,可以实现自动装箱拆箱,就是jdk里面会自动帮我们转换,不需要我们手动去强转,所以我们经常在这两种类型中随意写,平时也没什么注意 但Integer他是对象,我们知道 == 比较的是堆中的地址,但有个奇怪的事是, 如果 Integer a = 123, Integer b = 123,可以返回true,但如果Integer a = 12345, Integer b = 12345,返回false
public class Demo {
public static void main(String[] args) {
Integer c = -128;
Integer d = -128;
System.out.println("c == d: " + (c == d));
System.out.println("c.equals(d): " + c.equals(d));
System.out.println("c.intValue() == d.intValue(): " + (c.intValue() == d.intValue()));
System.out.println("Objects.equals(c, d): " + Objects.equals(c, d));
Integer e = 127;
Integer f = 127;
System.out.println("e == f: " + (e == f));
System.out.println("e.equals(f): " + e.equals(f));
System.out.println("e.intValue() == f.intValue(): " + (e.intValue() == f.intValue()));
System.out.println("Objects.equals(e, f): " + Objects.equals(e, f));
Integer g = 128;
Integer h = 128;
System.out.println("g == h: " + (g == h));
System.out.println("g.equals(h): " + g.equals(h));
System.out.println("g.intValue() == h.intValue():" + (g.intValue() == h.intValue()));
System.out.println("Objects.equals(g, h): " + Objects.equals(g, h));
}
}
结果如下:
c == d: true
c.equals(d): true
c.intValue() == d.intValue(): true
Objects.equals(c, d): true
e == f: true
e.equals(f): true
e.intValue() == f.intValue(): true
Objects.equals(e, f): true
g == h: false
g.equals(h): true
g.intValue() == h.intValue():true
Objects.equals(g, h): true
(1)当用“==”进行比较时,jvm默认是比较数据在java堆的地址。int是一种基本数据类型,jvm会自动将Integer转成int数值进行比较。在Integer类中,有一个内部静态类IntegerCache ,用来支持自动拆箱和装箱,如下,数值范围[-128,127]
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the -XX:AutoBoxCacheMax=<size> option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/ 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));
}
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() {}
}
默认IntegerCache.low 是-127,Integer.high是128,如果在这个区间[-128,127]内,他就会把变量i当做一个变量,放到内存中,用“==”比较是会得出true;但如果不在这个范围内,就会去new一个Integer对象,当运用“==”时,会比较Integer两个对象地址,得出false。
比较两个Integer的值是否相同,方法比较多:
1、推荐用equals(),这个还可以避免一些空指针问题的出现。
2、或者使用Integer.intValue();这样出来的就是int值,就可以直接比较了(可能会抛出空指针异常);
本文摘选两篇文章,略有修改
---------------------
作者:木林森淼
来源:CSDN
原文:https://blog.csdn.net/yangfengjueqi/article/details/81121140
---------------------
作者:不吃老鼠的小花猫
来源:CSDN
原文:https://blog.csdn.net/xiaojiesu/article/details/50215237
Java判断Integer类型的值是否相等的更多相关文章
- java中两个Integer类型的值相比较的问题
今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...
- Java判断文件类型
通常,在WEB系统中,上传文件时都需要做文件的类型校验,大致有如下几种方法: 1. 通过后缀名,如exe,jpg,bmp,rar,zip等等. 2. 通过读取文件,获取文件的Content-type来 ...
- Java 判断操作系统类型(适用于各种操作系统)
Java 判断操作系统类型(适用于各种操作系统) 最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以. 为了限制用户使用的操作系统,必须有统一的方法来获取才可 ...
- Java中Integer类型的整数值的大小比较
如果比较两个数值相等的Integer类型的整数,我们可能会发现,用"=="比较(首先你必须明确"=="比较的是地址),有的时候返回true,而有的时候,返回fa ...
- JAVA判断各种类型数据是否为空
1.判断list是否为空(Map.Set同list) if(list != null && list.size() == 0){ } if(list != null && ...
- java 判断int类型为空
int id = 10; if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(i ...
- [Java]判断Integer值相等最好不用==最好使用equals
测试代码 Integer c = ; Integer d = ; Integer e = ; Integer f = ; System.out.println(c == d); System.out. ...
- Java判断对象类型是否为数组
判断对象是否为数组: public static void main(String[] args) { String[] a = ["1","2"]; if(a ...
- Integer 类型数值判断相等的坑
题目: public static void main(String[] args) { Integer a = 100, b = 100; Integer c = 150, d = 150; Sys ...
随机推荐
- 页面直接导出为PDF文件,支持分页与页边距
将WEB页面直接导出为pdf文件是经常会用到的一个功能,尤其是各种报表系统.总结了一下目前几种主流的做法: 在后端用代码生成pdf文件,比如iText一类: 在后端抓取页面并生成pdf文件,比如pha ...
- 原生js与jquery加载页面元素比较
原生js:将获取元素的语句写到页面头部,会因为元素还没有加载而出错,js提供了window.onload 这个方法事先加载元素 <script type="text/javascrip ...
- Vue.js项目实战-多语种网站(租车)
首先来看一下网站效果,想写这个项目的读者可以自行下载哦,地址:https://github.com/Stray-Kite/Car: 在这个项目中,我们主要是为了学习语种切换,也就是右上角的 中文/En ...
- CSS样式表及选择器相关内容(二)-伪类与伪元素选择器
伪类与伪元素选择器归纳: 一.伪类选择器(伪类以":"开头,用在选择器后,用于指明元素在某种特殊的状态下才能被选中) 1.a标签伪类选择器,其他标签类似 eg: ...
- Java实现贪吃蛇游戏(含账号注册登录,排行榜功能)
这是我第一次工程实践的作业,选题很多,但我只对其中的游戏开发感兴趣,可游戏就两三个类型,最终还是选择了贪吃蛇.其实就贪吃蛇本身的代码实现还算是比较简单的,可是实践要求代码行达到一定数量,所以我就额外给 ...
- github 分支管理
github 分支管理 最近有同事问我git 如何管理分支,这里我以github为例,做下工作中常用的分支管理操作. 分支管理 作用:假设你准备开发一个新功能,但需要两周才能完成,第一周写了60%,如 ...
- 微信支付:chooseWXPay:fail, the permission value is offline verifying
在开发公众号微信支付的时候,到了支付那一步,页面上看到微信支付的loading一闪而过,但是没有出现微信支付的页面.控制台log显示错误信息:“chooseWXPay:fail, the permis ...
- day04 作业
一.简述Python的五大数据类型的作用.定义方式.使用方法: 数字类型 整型 作用:描述年龄 定义方式: x = 10 y = int('10') 使用方法: + - * / % // ** 如果需 ...
- CSS3 边框 border-image
border-image:xx xx xx 是一系列参数的简写,该属性将图片作为边框修饰 border-image-source:url(border.png); 图片url地址 border-ima ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...