当前位置:网站首页>为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
2022-04-23 11:57:00 【我是一棵卷心菜】
大家好,我是卷心菜,可以叫我菜菜(名副其实了属于是,哈哈~),大二学生一枚。本篇主要讲解Java面试题:包装类以及相关的知识。如果您看完文章有所收获,可以三连支持博主哦~,嘻嘻。
一、前言
- 开门见山,首先看看八种基本数据类型对应的包装类:
| 基本数据类型 | 包装类 |
|---|---|
| char | Character |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
其中Character 、Boolean的父类是Object,其余的父类是Number
二、装箱和拆箱
- 自动装箱和拆箱就是将基本数据类型和包装类之间进行自动的互相转换。JDK1.5后,Java引入了自动装箱(autoboxing)/拆箱(unboxing)。
- 自动装箱:在JDK1.5以前,代码
Integer i = 10是错误的,必须要通过Integer i = new Integer(10)这样的语句来实现基本数据类型转换成包装类的过程;而在JDK1.5以后,Java提供了自动装箱的功能,自动装箱过程是通过调用包装类的valueOf()方法实现的,因此只需Integer i = 10这样的语句就能实现基本数据类型转换成包装类,这是因为JVM为我们执行了Integer i = Integer.valueOf(10)这样的操作。
public void test1() {
Integer a = 10;
//等价于 Integer a = Integer.valueOf(10);
}
- 自动拆箱:每当需要一个值时,对象会自动转成基本数据类型,没必要再去显式调用
intValue()、doubleValue()等转型方法。自动拆箱过程是通过调用包装类的 xxxValue()方法实现的
public void test1() {
int a = new Integer(10);
//等价于int a = new Integer(10).intValue();
}
三、空指针异常
既然知道了装箱和拆箱,那么下面的代码,我们就可以很明白的分析出来为什么会报空指针的异常了:
public void test1() {
Integer a = null;
int b = a;
}

四、转换的使用
前面已经讲解了基本数据类型以及包装类之间的相互转换,即自动装箱和拆箱;下面看看基本数据类型与String类之间的转换、String类与包装类之间的转换。
1、基本数据类型与String类之间的转换
- 基本数据类型到String类:调用valueOf()
public void test1() {
int a = 3;
String s = String.valueOf(a);
System.out.println(s.getClass());
//class java.lang.String
}

- String类到基本数据类型:调用parseXxx()
public void test2() {
String s = "12";
int a = Integer.parseInt(s);
}

2、String类与包装类之间的转换
- String类到包装类:使用构造器
public void test3() {
String s = "12";
Integer a = new Integer(s);
System.out.println(a.getClass());
//class java.lang.Integer
}
- 包装类到String类:
public void test4() {
Integer a = new Integer(12);
String s = a.toString();
String s1 = Integer.toString(a);
System.out.println(s.getClass());
System.out.println(s1.getClass());
//class java.lang.String
}
五、经典面试题
试着写一写,看看自己会不会:
@Test
public void test5() {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);
Integer m = 1;
Integer n = 1;
System.out.println(m == n);
Integer x = 128;
Integer y = 128;
System.out.println(x == y);
}
输出结果:false、true、false
为什么是这样?在这里推荐一篇文章,写的是真的棒:https://www.cnblogs.com/dolphin0520/p/3780005.html
六、总结
用一张图片来收尾~

感谢阅读,一起进步,嘻嘻~
版权声明
本文为[我是一棵卷心菜]所创,转载请带上原文链接,感谢
https://cabbage.blog.csdn.net/article/details/124325181
边栏推荐
- Significance of actively participating in middle school robot competition
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Siri gave the most embarrassing social death moment of the year
- Nacos Foundation (8): login management
- Database design of forum system
- Tensorflow uses keras to create neural networks
- WIN10 启动后花屏
- On lambda powertools typescript
- MQ is easy to use in laravel
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
猜你喜欢

Yunna | how to manage the company's fixed assets and how to manage fixed assets

IDEA 中 .properties文件的中文显示乱码问题的解决办法

How to count fixed assets and how to generate an asset count report with one click

Tensorflow使用keras创建神经网络的方法

激活函数之阶跃函数

外包干了五年,废了...

PSCP basic usage

解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)

Redis学习之五---高并发分布式锁实战

Analyzing the role of social robots in basic science
随机推荐
远程访问家里的树莓派(上)
Nacos Foundation (7): Configuration Management
Chapter 5 optimizing queries using in memory expressions (IM 5.1)
全网最细的短网址系统设计与实战
SOFA Weekly | 年度优秀 Committer 、本周 Contributor、本周 QA
WIN10 启动后花屏
运行报错:找不到或无法加载主类 com.xxx.Application
Castle.DynamicProxy实现事务单元控制
RebbitMQ的初步了解
初探 Lambda Powertools TypeScript
Analyze the rules for the use of robots with good performance
Database design of forum system
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
Here comes the detailed picture and text installation tutorial of H5 game
Understanding of MQ
科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智
How does QT turn qwigdet into qdialog
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
On lambda powertools typescript
Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind