浅谈Java中Int、Integer、Integer.valueOf()、new Integer()之间的区别
作者:feng的记忆 发布时间:2023-10-29 20:08:53
Int
Int是Java八种基本数据类型之一,一般大小为4字节32位,取值范围为2-31—231。两个Int类型变量用“==”比较的是值的大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
int b = 100;
System.out.println(a == b);//true
}
}
Integer和Integer.valueOf()
将Int值赋给Integer变量,系统会自动将这个Int值封装成一个Integer对象。
比如:Integer a = 100;实际上的操作是:Integer a = Integer.valueOf(100);
以下是valueOf()的源码
注意:这里Integer.valueOf(),当Int值的范围在-128-127之间时,会通过一个IntegerCache缓存来创建Integer对象;当Int值不在该范围时,直接调用new Integer()来创建对象,因此会出现以下的情况
(1)Integer a = 100; Integer b = 100; a==b结果为true,因为这两个Integer变量引用的是缓存中的同一个Integer对象 ;
(2)Integer c = 200; Integer d = 200; a==b结果为false,因为a和b是通过new Integer() 创建的两个不同对象。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a == b);//true
System.out.println(c == d);//false
}
}
new Integer()
new Integer()每次都会创建新的对象,==比较的是两个对象的内存地址
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = new Integer(100);
Integer b = new Integer(100);
System.out.println(a == b);//false
}
}
三者之间的比较
(1)不管是new创建的Integer对象,还是通过直接赋值Int值创建的Integer对象,它们与Int类型变量通过“==”进行比较时都会自动拆箱变成Int类型,所以Integer对象和Int变量比较的是内容大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
Integer b = 100;//等价于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(a == b);//true
System.out.println(a == c);//true
}
}
(2)new创建的Integer对象和直接赋Int值创建的Integer对象使用==比较的是它们的内存地址。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer b = 100;//等价于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(b == c);//false
}
}
(3)赋Int值创建的Integer对象比较:
当Int值在-128-127之间时,两个Integer变量引用的是IntegerCache中的同一个对象,内存地址相同,因此==的结果为true;
当Int值不在以上范围时,两个Integer对象都是通过new创建的,内存地址不同,因此==的结果为false
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
Integer f = new Integer(100);
System.out.println(a == b);//true
System.out.println(c == d);//false
System.out.println(a == f);//false
}
}
一个金典面试题
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 49;
int b = 49;
Integer c = Integer.valueOf(49);
Integer d = new Integer(49);
System.out.println(a == b);//true
System.out.println(a == c);//true
System.out.println(b == c);//true
System.out.println(c == d);//false
}
}
来源:https://blog.csdn.net/qq_40742428/article/details/121183574


猜你喜欢
- 一、安装jdk1.查看Linux自带的JDK是否已安装java –version如果出现openjdk,最好还是先卸载掉openjdk,在安
- Android 想判断 Activity 是否是全屏,网上找了些方法,看到有直接获取 flags 和一个具体的值比较,并没有用,其实分析下来
- 最近项目里面做了一个定时器,结果报错这个。网上的原因大多说是什么版本问题。我记录下我的问题所在。由于项目启动在局域网,不能访问互联网。打出来
- 目录背景实体类示例一示例二背景以前常用的排序方式是通过实现Comparator接口来进行排序,写法相对来说比较复杂,使用Comparator
- SLF4J是一个日志框架抽象层,底下绑定具体的日志框架,比如说Log4J,Logback,Java Logging API等。SLF4J也有
- 一.服务端代码:import java.net.*; // for Socket, ServerSocket, and InetAddres
- java 中设计模式(值对象)的实例详解应用场景:在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值,该如
- 上周,公司的项目改版要求加上一个右滑返回上一个界面,于是就在网上找了一些开源库打算实现.但是在使用的时候遇见了许多的问题.试了两天用过 ht
- 前言Go 语言比 Java 语言性能优越的一个原因,就是轻量级线程Goroutines(协程Coroutine)。本篇文章深入分析下 Jav
- 本文实现了一个有趣的小东西:使用自定义View绘图,一边画线,画出的线条渐渐变淡,直到消失。效果如下图所示:用属性动画或者渐变填充(Shad
- 理论基础so的加载是一种解析式装载,这与dex有一定区别,dex是先加载进行优化验证生成odex,再去解析odex文件,而so更像边解析边装
- 在前面的文章中,我们分析了淘宝android客户端的一些界面实现和用户体验,今天这篇文章,主要介绍如何使用自定义控件,实现抢购倒计时的功能。
- 本文实例讲述了C#编程简单实现生成PDF文档的方法。分享给大家供大家参考,具体如下:using System;using System.IO
- 本文实例讲述了Java实现字符串解析为日期时间的方法。分享给大家供大家参考,具体如下:Java版本:1.8开始import java.tim
- 下面还有投票,帮忙投个票👍前言最近在看某个开源项目代码并准备参与其中,代码过了一遍后发现多个自定义的配置文件用来装载业务配置代替数据库查询,
- 一.继承的类型在面向对象的编程中,有两种截然不同继承类型:实现继承和接口继承1.实现继承和接口继承*实现继承:表示一个类型派生于基类型,它拥
- 前言平时做一些统计数据,经常从数据库或者是从接口获取出来的数据,单位是跟业务需求不一致的。比如, 我们拿出来的 分, 实际上要是元又比如,我
- 今天一个读者问我关于Android通过调用Webservice实现天气预报这篇文章的源码下载后出现的错误Could not find cla
- Android使用GridView实现日历功能示例,代码有点多,发个图先:如果懒得往下看的,可以直接下载源码吧,最近一直有人要,由于时间太久
- 面试官:请问StringBuffer和StringBuilder有什么区别?这是一个老生常谈的话题,笔者前几年每次面试都会被问到,作为基础面