当前位置:网站首页>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 = 10It'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 = 10Such 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
边栏推荐
- docker MySQL主从备份
- Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
- Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
- Array---
- Interpretation of biological recognition in robot programming course
- 同态加密技术学习
- 画结果图推荐网址
- 关于使用Go语言创建WebSocket服务浅谈
- 数组---
- oh-my-lotto
猜你喜欢

C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭

怎么进行固定资产盘点,资产盘点报告如何一键生成

VMware虚拟机使用esxi 导出硬盘vmdk文件

Running error: unable to find or load the main class com xxx. Application

How to expand the capacity of the server in the 100 million level traffic architecture? Well written!

Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years

一文详解头部位姿估计【收藏好文】

5个免费音频素材网站,建议收藏

Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied

魔域来了H5游戏详细图文架设教程
随机推荐
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
c# 设置logo图标和快捷方式的图标
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
PSCP 基本使用
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
同态加密技术学习
Purpose of IM expression (IM 5.2)
关于使用Go语言创建WebSocket服务浅谈
In idea Solution to the problem of garbled code in Chinese display of properties file
远程访问家里的树莓派(上)
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
Chapter 4 is a tutorial on forced filling of in memory objects with IM enabled filling objects (IM 4.7)
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
智能多线弹性云增加独立的IP地址,如何实现多线功能?
VMware virtual machines export hard disk vmdk files using esxi
Analyzing the role of social robots in basic science
Nacos Foundation (9): Nacos configuration management from single architecture to microservices
SOFA Weekly | 年度优秀 Committer 、本周 Contributor、本周 QA
力扣-70.爬楼梯