当前位置:网站首页>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.
边栏推荐
猜你喜欢
003_旭日X3派初探:利用无线串口通信控制舵机
神经网络之感知机
自监督论文阅读笔记Index Your Position: A Novel Self-Supervised Learning Method for Remote Sensing Images Sema
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery
自监督论文阅读笔记DisCo: Remedy Self-supervised Learning on Lightweight Models with Distilled Contrastive
【DC-5 Range Penetration】
Kettle 从资源库中载入新的转换出错(Invalid byte 1 of 1-byte UTF-8 sequence)
SolidWorks 操作视频 | 流体分析结果演示
IPC 通信 - IPC
ZEMAX | 绘图分辨率结果对光线追迹的影响
随机推荐
cb板上常用的电子元器件都有哪些?
九、请介绍类加载过程,什么是双亲委派模型?
ucos任务调度原理
自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
A.1#【内存管理】——1.1.3 page: struct page
自监督论文阅读笔记 Ship Detection in Sentinel 2 Multi-Spectral Images with Self-Supervised Learning
神经网络之感知机
稳压二极管的工作原理及稳压二极管使用电路图
电容器和电池有什么不同?
ZEMAX | 如何创建简单的非序列系统
自监督论文阅读笔记 DenseCL:Dense Contrastive Learning for Self-Supervised Visual Pre-Training
浮点型数据在内存中存储的表示
C# 数组之回溯法
Qlik Sense 赋值详解(Set、Let)
ZEMAX | 如何使用ZOS-API创建自定义操作数
memblock
ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
ASP.NET MVC3的伪静态实现
C# Base64加密
自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线