当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
php连接数据库脚本
进程间通信IPC - 信号量
enum和enum class的区别
东南亚跨境电商
003_旭日X3派初探:利用无线串口通信控制舵机
深度学习基本概念
Kotlin 中的泛型介绍
自监督论文阅读笔记 DenseCL:Dense Contrastive Learning for Self-Supervised Visual Pre-Training
自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线
HANA 常用数据类型详解
建立平衡二叉树简单demo
Qlik Sense 判空详解(IsNull)
二阶段提问总结
嵌入汇编-1 格式讲解
自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection
Android学习 | 08.SQLiteOpenHelper
九、请介绍类加载过程,什么是双亲委派模型?
softmax和最大熵
Gradle插件与代理服务器导致Sync Project失败的问题
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery








