当前位置:网站首页>为什么要有包装类,顺便说一说基本数据类型、包装类、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
边栏推荐
- Redis optimization series (II) redis master-slave principle and master-slave common configuration
- Analyzing the role of social robots in basic science
- 5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
- 什么是网关
- 简易投票系统数据库设计
- Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
- Next.js 静态数据生成以及服务端渲染的方式
- golang之笔试题&面试题01
- Chapter 5 optimizing queries using in memory expressions (IM 5.1)
- Nacos Basics (5): getting started with Nacos configuration
猜你喜欢
Significance of actively participating in middle school robot competition
Redis学习之五---高并发分布式锁实战
Interpretation of 2022 robot education industry analysis report
C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
Tensorflow使用keras创建神经网络的方法
Here comes the detailed picture and text installation tutorial of H5 game
Analyze the rules for the use of robots with good performance
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
Nacos Basics (5): getting started with Nacos configuration
如果你是一个Golang面试官,你会问哪些问题?
随机推荐
激活函数之sigmoid函数
Force buckle - 70 climb stairs
运行报错:找不到或无法加载主类 com.xxx.Application
How Im expressions work (5.3)
PSCP 基本使用
IDEA 中 .properties文件的中文显示乱码问题的解决办法
Laravel adds custom helper functions
Design and practice of the smallest short website system in the whole network
Precautions for PCB
Yunna | how to manage the company's fixed assets and how to manage fixed assets
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
How imeu is associated with imcu (IM 5.5)
How does QT turn qwigdet into qdialog
1. Construction of electron development environment
Maker education for primary and middle school students to learn in happiness
配电房远程综合监控系统在10kV预制舱项目中的应用
Sigmoid function of activation function
IM表达式如何工作(5.3)
1.Electron开发环境搭建
Tensorflow uses keras to create neural networks