Integer和int使用==比较的总结
public static void main(String[] args) {
int i1 = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println("**************************************");
Integer i4 = 127;
Integer i5 = 127;
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i4 == i5);//true
System.out.println(i6 == i7);//false
System.out.println("**************************************");
Integer i8 = new Integer(127);
Integer i9 = new Integer(127);
System.out.println(i8 == i9);//false
System.out.println(i8.equals(i9));//true
System.out.println(i4 == i8);//false
/* Output:
true
true
**************************************
true
false
**************************************
false
true
false
*/
}
- 第5和第6行的结果都为true。因为Integer与int比较时,Ingeger都会自动拆箱(jdk1.5以上)。
- 第12行结果为true,第13行结果为false。
因为Java在编译的时候,Integer i4=127被翻译成-> Integer i4= Integer.valueOf(127);
JDK源码:/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}看一下源码大家就会明白,对于-128到127之间的数,会进行缓存,Integer i6 = 127时,会将127进行缓存,下次再写Integer i7 = 127时,就会直接从缓存中取,就不会new了。
- i8、i9使用的是new, 对象不一样,所以第17行结果为false,第18行结果为true ,第19行结果为false。
总结
- Ingeter是int的包装类,int的初值为0,Ingeter的初值为null。
- 无论如何,Integer与new Integer()不会相等。不会经历拆箱过程,i8的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,使用 == 比较都为false。
- 两个都是非new出来的Integer,使用 == 比较,如果数在-128到127之间,则是true,否则为false
- 两个都是new出来的,==比较都为false。若要比较值是否相等,需使用equals方法进行比较。
- int和Integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比。
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
Integer和int使用==比较的总结的更多相关文章
- Integer与int的区别(包装类和基本数据类型的区别)
1. 默认值 int默认值为0,Integer的默认值为null.推论:Integer既可以表示null又可以表示0 2. 包装类中提供了该类型相关的很多算法操作方法 如把十进制装换为2进制(toBi ...
- Integer与int的区别
简述:int与Integer的区别: 对于它们,我们可能只是知道简单的区别.Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null.但是他们之间真的仅仅只有这些区别 ...
- Integer与int的种种比较
package com.lxm.basics; public class IntegerTest { public static void main(String[] args) { Integer ...
- Integer与int的种种比较你知道多少?
如果面试官问Integer与int的区别:估计大多数人只会说道两点,(1)Ingeter是int的包装类,int的初值为0:(2)Ingeter的 初值为null.但是如果面试官再问一下Integer ...
- java Integer和int的拆箱与装箱
官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...
- Java数据类型中String、Integer、int相互间的转换
1.Integer转换成int的方法 Integer i; int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Int ...
- Integer和int的详细比较(转)
Integer与int的区别我们耳熟详的有两点:1.Integer是int的包装类.2.Integer的默认初始值是null,而int的默认初试值是0. 下面通过代码进行详细比较. public cl ...
- Integer.valueOf(int)及自动装箱内幕
Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...
- 关于Integer与int
integer a=new integer(1); integer b=new integer(1); int c=1; integer d=1; a==b false因为地址不同: a==c t ...
- integer与int区别以及integer.values()方法详解
声明:本文为博主转载文章,原文地址见文末. 知识点1:integer和int的区别 /* * int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为 ...
随机推荐
- (转)使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- Java取得一个对象里所有get方法和set方法, 读取某个类下所有变量的名称
所有get方法和set方法public void getMethod(Object obj){ Class clazz=obj.getClass();//获得实体类名 Field[] fields = ...
- Day6 下(
T1 模拟,80? #include<iostream> #include<cstring> #include<queue> #include<algorit ...
- avalon教程-简介
avalon是什么? avalon是一个MVVM框架, Modle-模型层,即为js中从后台接口中取出的数据,例如一个对象或者对象数组,并对这些数据进行一定的格式化.常见的返回数据是这样的{id : ...
- 多ComboBox实现复杂查询
关键是,你是要实现什么功能:是四个条件都有内容时查询,还是哪个内容有查哪个? 如果四个组合框都有内容,相对简单些: string s = "select * from 表名 where 身份 ...
- 合并Gridview单元格
Introduction There are a lot of methods in the Internet solving the problem of how to merge GridView ...
- Lombok使用与问题
前言 想想已经工作了一年,工作中遇到的问题一直没有记录下来,以后遇到相同的问题可能还需要花费很多的时间,因此打算记录一下博客.方便以后自己的复习和问题查找 刚好最近项目引入了Lombok,刚好从现在起 ...
- mysql-数据库模式定义语言(DDL)
库的管理 /* 一.库的管理 创建.修改.删除 二.表的管理 创建.修改.删除 创建: create 修改: alter 删除: drop */ #一.库的管理 #.库的创建 /* 语法: creat ...
- 用IDEA创建一个SpringBoot项目
next后等待项目构建完成 运行方法一: 方法二:
- MySQL(四)
一.使用正则表达式查询 SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEX ...