当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Gradle插件与代理服务器导致Sync Project失败的问题
神经网络基础
VCC(电源)和 GND(地)之间电容的作用
自监督论文阅读笔记Index Your Position: A Novel Self-Supervised Learning Method for Remote Sensing Images Sema
ZEMAX | 如何创建复杂的非序列物体
ARMv8 架构----armv8 类别
cmdline -[command line,__fdt_pointer,initial_boot_params] boot_command_line 获取
Qemu 搭建Armv8 平台
设备树解析源码分析<devicetree>-1.基础结构
opencv目标检测
Dynamic adjustment subject web system?Look at this one is enough
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery
Kotlin 中的泛型介绍
Mysql 外键详解(Foreign Key)
自监督论文阅读笔记 Self-Supervised Visual Representation Learning with Semantic Grouping
Oracle 日历表详解(含节假日)
浮点型数据在内存中存储的表示
自监督论文阅读笔记 Incremental-DETR:Incremental Few-Shot Object Detection via Self-Supervised Learning
MATLAB给多组条形图添加误差棒
对象の使用