当前位置:网站首页>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
边栏推荐
- 激活函数之relu函数
- Nacos Foundation (7): Configuration Management
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- How imeu is associated with imcu (IM 5.5)
- Nacos Foundation (6): Nacos configuration management model
- IDEA 数据库插件Database Navigator 插件
- 初探 Lambda Powertools TypeScript
- 第四章 为IM 启用填充对象之为IM列存储启用ADO(IM 4.8)
- 使用连接组优化连接 (IM 6)
- Database design of simple voting system
猜你喜欢
Overall plan management mode in maker Education
Tips for installing MySQL service in windows11: Install / Remove of the Service denied
一文详解头部位姿估计【收藏好文】
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
IDEA设置版权信息
Use kettle to copy records to and get records from results
运行报错:找不到或无法加载主类 com.xxx.Application
Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
力扣-1137.第N个泰波那契数
Force buckle - 70 climb stairs
随机推荐
Chapter 4 is a tutorial on forced filling of in memory objects with IM enabled filling objects (IM 4.7)
Force buckle - 1137 Nth teponacci number
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
ImportError: libX11. so. 6: cannot open shared object file: No such file or directory
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
SOFA Weekly | 年度优秀 Committer 、本周 Contributor、本周 QA
Tensorflow uses keras to create neural networks
worder字体网页字体对照表
第二十六课 类的静态成员函数
The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
Golang's pen test questions & interview questions 01
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
同态加密技术学习
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
IM表达式如何工作(5.3)
智能多线弹性云增加独立的IP地址,如何实现多线功能?
Nacos Foundation (8): login management
程序员如何用130行代码敲定核酸统计
远程访问家里的树莓派(上)
用户接口和IM表达式(IM 5.6)