当前位置:网站首页>5. What is the difference between int and Integer?
5. What is the difference between int and Integer?
2022-08-03 06:10:00 【Shiyu】
One. Basic usage comparison
int is a basic data type, and the default value is 0 when used as a member variable.
Integer is a wrapper class for int. When used as a member variable, the default value is null.
int can be used directly without instantiation, and Integer can be used after instantiation.
int stores the value directly, while Integer is a reference to the object.
2. Compare size processing (using ==)
Usually compare the same numbers:
1. An int and an Integer:
int a=5;Integer b=5;a==b;//true
Integer objects are automatically unboxed and compared to int data.Return true.
2. Two new Integers: because they are both new objects, they are actually two different objects, not equal.Return false.
Integer a=new Integer(5);Integer b=new integer(5);a==b;//false
3. One new and one not new: In fact, it is still a comparison of objects, and it should be that different objects are not equal.return false;
Integer a=5;Integer b=new Integer(5);a==b;//false
4. Neither are new
The first case: the value is between -128-127, return true
Integer a=5;Integer b=5;a==b;//true
The second case: the value is not between the above ranges, return false
Integer a=300;integer b=300;a==b;//false
Reason: When the value is between -128-127, Integer a=5; it will be translated as Integer i = Integer.valueOf(5).The definition of valueOf of Integer type in the java API is as follows. For numbers between -128 and 127, it will be cached. When Integer a=5, 5 will be cached, and when Integer b=5 is written next time, it will be cached.It will be taken directly from the cache, and there will be no new.
边栏推荐
猜你喜欢
PCB设计经验之模拟电路和数字电路区别为何那么大
深度学习基本概念
MySql的Sql语句的练习(试试你能写出来几道呢)
003_旭日X3派初探:利用无线串口通信控制舵机
神经网络之感知机
ZEMAX | 如何使用ZOS-API创建自定义操作数
电容器和电池有什么不同?
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection
进程间通信IPC - 信号量
随机推荐
ARMv8 架构----armv8 类别
[frp intranet penetration]
什么是参数化设计,通过实操了解一下? | SOLIDWORKS 操作视频
深度学习理论课程第八、九、十章总结
VSCODE 常见问题
自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
ucosII OSMemCreate()函数的解析
自监督论文阅读笔记 Incremental-DETR:Incremental Few-Shot Object Detection via Self-Supervised Learning
自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线
二、Exception和Error有什么区别?
快速的将结构体各成员清零
常见的电子元器件分类介绍-唯样商城
关于芯片你了解吗?
损失函数(第五周)
g++参数说明
Kettle Spoon 安装配置详解
关于梯度下降法的一些优化方法
Kotlin 中的泛型介绍
电子元器件和电子元件的区别有那些?
常见的电子元器件分类介绍