当前位置:网站首页>Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
2022-04-23 12:05:00 【I am a cabbage】
Hello everyone , I'm cabbage , You can call me vegetable ( Worthy of the name, it belongs to , ha-ha ~), A sophomore . This article mainly explains Java Interview questions : Packaging and related knowledge . If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .
List of articles
One 、 Preface
- Come to the point , First, let's take a look at the packaging classes corresponding to the eight basic data types :
Basic data type | Packaging |
---|---|
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
among Character
、Boolean
The parent class is Object, The remaining parent classes are Number
Two 、 Packing and unpacking
- Automatic packing and unpacking is the automatic conversion between basic data types and packaging classes .JDK1.5 after ,Java Introduced automatic boxing (autoboxing)/ Unpacking (unboxing).
- Automatic boxing : stay JDK1.5 before , Code
Integer i = 10
It's wrong. , Must passInteger i = new Integer(10)
Such statements are used to realize the process of converting basic data types into wrapper classes ; And in the JDK1.5 in the future ,Java It provides the function of automatic packing , The automatic boxing process is performed by calling the wrapper classvalueOf()
Method , So justInteger i = 10
Such a statement can convert the basic data type into a wrapper class , This is because JVM For us Integer i = Integer.valueOf(10) This kind of operation .
public void test1() {
Integer a = 10;
// Equivalent to Integer a = Integer.valueOf(10);
}
- Automatic dismantling : Whenever a value is needed , The object is automatically converted to the basic data type , There is no need to explicitly call
intValue()
、doubleValue()
And other transformation methods . The automatic unpacking process is implemented by calling the wrapper class xxxValue() Method
public void test1() {
int a = new Integer(10);
// Equivalent to int a = new Integer(10).intValue();
}
3、 ... and 、 Null pointer exception
Now that you know packing and unpacking , So the following code , We can clearly analyze why the null pointer exception is reported :
public void test1() {
Integer a = null;
int b = a;
}
Four 、 The use of transformation
The basic data types and the conversion between wrapper classes have been explained earlier , That is, automatic packing and unpacking ; Let's take a look at the basic data types and String Conversion between classes 、String Conversion between class and wrapper class .
1、 Basic data types and String Conversion between classes
- Basic data type to String class : call valueOf()
public void test1() {
int a = 3;
String s = String.valueOf(a);
System.out.println(s.getClass());
//class java.lang.String
}
- String Class to basic data type : call parseXxx()
public void test2() {
String s = "12";
int a = Integer.parseInt(s);
}
2、String Conversion between class and wrapper class
- String Class to packaging class : Using constructors
public void test3() {
String s = "12";
Integer a = new Integer(s);
System.out.println(a.getClass());
//class java.lang.Integer
}
- Packaging class to String class :
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
}
5、 ... and 、 Classic interview questions
Try to write , See if you can :
@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);
}
Output results :false
、true
、false
Why is that ? Recommend an article here , It's really great :https://www.cnblogs.com/dolphin0520/p/3780005.html
6、 ... and 、 summary
End with a picture ~
Thank you for reading , Progress together , Hee hee ~
版权声明
本文为[I am a cabbage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231156341996.html
边栏推荐
- 宝塔面板命令行帮助教程(包含重置密码)
- IDEA 代码格式化插件Save Actions
- SQL 练习(一)
- Im architecture: CPU architecture: SIMD vector processing (im-2.3)
- [redis series] redis learning 13. Redis often asks simple interview questions
- 数据库如何填充IM表达式(IM 5.4)
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- Golang Pen & interview 01
- Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
- User interface and im expression (IM 5.6)
猜你喜欢
力扣-1137.第N个泰波那契数
【Redis 系列】redis 学习十三,Redis 常问简单面试题
1.Electron开发环境搭建
Database design of simple voting system
抓包整理————tcp 协议[八]
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
Force buckle - 70 climb stairs
VMware virtual machines export hard disk vmdk files using esxi
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
Nacos Foundation (6): Nacos configuration management model
随机推荐
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
软件测试对于减少程序BUG有多大帮助?
Database design of forum system
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
异步时钟亚稳态 的解决方案——多bit信号
什么是网关
同态加密技术学习
After a circle, I sorted out this set of interview questions..
Link sorting of tutorials such as assembly language running environment setting
数组---
第四章 为IM 启用填充对象之强制填充In-Memory对象:教程(IM 4.7)
Nacos Foundation (9): Nacos configuration management from single architecture to microservices
Share two practical shell scripts
Relu function of activation function
如果你是一个Golang面试官,你会问哪些问题?
Fastjson 2 来了,性能继续提升,还能再战十年
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
VMware virtual machines export hard disk vmdk files using esxi
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
电脑系统卡如何解决?