当前位置:网站首页>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;//trueInteger 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;//false3. 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;//false4. Neither are new
The first case: the value is between -128-127, return true
Integer a=5;Integer b=5;a==b;//trueThe second case: the value is not between the above ranges, return false
Integer a=300;integer b=300;a==b;//falseReason: 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.
边栏推荐
猜你喜欢
随机推荐
常见的电子元器件分类介绍
自监督论文阅读笔记 SimCLRV2 Big Self-Supervised Models are Strong Semi-Supervised Learners
使用JSP实现简单的登录注册功能,并且使用Session跟踪用户登录信息
IPC 通信 - IPC
Dynamic adjustment subject web system?Look at this one is enough
Makefile自动推导的简单例程
自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
电子元器件的分类有哪些?
KASLR-内核地址空间布局随机化
ZEMAX | 如何使用ZOS-API创建自定义操作数
ZEMAX | 绘图分辨率结果对光线追迹的影响
电子元器件之电子变压器可分为哪几类?
ZEMAX | 探究 OpticStudio 偏振分析功能
Mysql 存储过程详解(procedure)
003_旭日X3派初探:利用无线串口通信控制舵机
g++参数说明
ZEMAX | 在设计抬头显示器(HUD)时需要使用哪些工具?
自监督论文阅读笔记 Self-supervised Learning in Remote Sensing: A Review
采用Trench肖特基二极管,实现功率密度的显著提升
观看华为AI技术领域课程--深度学习前三章总结








