当前位置:网站首页>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
边栏推荐
- In idea Solution to the problem of garbled code in Chinese display of properties file
- 画结果图推荐网址
- How imeu is associated with imcu (IM 5.5)
- C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
- ThinkPHP adds image text watermark to generate promotion poster with QR code
- 面了一圈,整理了这套面试题。。
- Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
- 程序员如何用130行代码敲定核酸统计
- 数据库如何填充IM表达式(IM 5.4)
- Use kettle to copy records to and get records from results
猜你喜欢

激活函数之阶跃函数

IDEA设置版权信息

1.Electron开发环境搭建

kettle复制记录到结果和从结果获取记录使用

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

Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project

编程辅助工具推荐:图片工具snipaste

【Redis 系列】redis 学习十三,Redis 常问简单面试题

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

运行报错:找不到或无法加载主类 com.xxx.Application
随机推荐
Idea code formatting plug-in save actions
After a circle, I sorted out this set of interview questions..
Running error: unable to find or load the main class com xxx. Application
SOFA Weekly | 年度优秀 Committer 、本周 Contributor、本周 QA
SQL 练习(一)
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
远程访问家里的树莓派(上)
电脑系统卡如何解决?
异步时钟亚稳态 的解决方案——多bit信号
Link sorting of tutorials such as assembly language running environment setting
Design and practice of the smallest short website system in the whole network
kettle复制记录到结果和从结果获取记录使用
On lambda powertools typescript
智能多线弹性云增加独立的IP地址,如何实现多线功能?
云呐|如何管理好公司的固定资产,固定资产管理怎么做
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
Summary of convolution layer and pooling layer
亿级流量架构,服务器如何扩容?写得太好了!
Exploring the equipment and teaching of robot education
魔域来了H5游戏详细图文架设教程