当前位置:网站首页>包装类
包装类
2022-04-21 18:01:00 【RhineHe_hjs】
包装类
包装类的引入原因
我们都知道java是纯面向对象编程,但是其中包含了8中基本数据类型,它们是创建对象的,所以就引入了包装类,对应了八种基本数据类型,以下就是八种基本类型对应的包装类。
- byte -->Byte
- char -->Character
- short -->Short
- int -->Integer
- float -->Float
- double -->Double
- long -->Long
- boolean -->Boolean
包装类的方法
- 手动包箱:对应包装类.valueof(data);
Integer integer = Integer.valueOf(100);
Character character =Character.valueOf('c');
- 手动拆箱:
int num =integer.intValue();
char ch = character.charvalue();
这里呢我就只列举这两个,其他的类型也如上述使用即可,除了使用包装类提供的方法进行包箱与拆箱外,java也为我们提供了自动包箱与拆箱机制。
自动包箱与拆箱
- 自动包箱
Integer integer2 =12;
- 自动拆箱
int num2 = integer;
自己测试所写代码
public class PInteger {
public static void main(String[] args) {
//手动包箱-->>对应包装类.valueOf(包箱数据);
Integer integer = Integer.valueOf(100);
Character character =Character.valueOf('c');
System.out.println("包箱后:"+integer);
System.out.println("包箱后:"+character);
// 手动拆箱-->>包装类对象.valueof();
int num =integer.intValue();
char ch =character.charValue();
System.out.println("拆箱后:"+num);
System.out.println("拆箱后:"+ch);
// 自动包箱
Integer integer2 =12;
Character character2 = 'a';
System.out.println("自动包箱后:"+integer2);
System.out.println("自动包箱后:"+character2);
// 自动拆箱
int num2 = integer;
char ch2 =character;
System.out.println("自动拆箱后:"+num2);
System.out.println("自动拆箱后:"+ch2);
}
}
提示
以上只是我自己的看法,如有相同纯属意外。如有错误,也请谅解,勿喷!如有收获或疑问,欢迎点赞评论!
版权声明
本文为[RhineHe_hjs]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yigan123/article/details/106571952
边栏推荐
- Logstash ~ filter of logstash
- onnx效率问题:和Module & DataParallel比较
- Linux~libc.so.6(GLIBC_2.28)(64bit) 被 redis-5.0.3-1.nfs.x86_64 需要
- Interface test framework practice (II) | interface request assertion
- Variable与Tensor合并后,关于训练、验证的相关变化
- "Industrial Internet plus safety production" to enhance the safety level of industrial enterprises
- Laxcus distributed operation redundancy and fault tolerance node
- Linux~libc. so. 6 (glibc_2.28) (64bit) is redis-5.0.3-1 nfs. x86_ 64 required
- MySQL query table field default value
- 看完这篇教程,你将拥有自己的一个卫星(diy全程详解)
猜你喜欢

MySQL localization workbench localization XML file

Ase35p03-asemi FET 35p03

看看项目经理是如何把一个项目带崩的

头条自媒体运营秘籍,坚持下去你就可以打败90%的人

It is claimed that MNIST and cifar 10 have been "solved" and 100% accuracy has been achieved

Headline we media operation secret script. If you stick to it, you can beat 90% of the people

MySQL basic collection

The upper computer is fun to play like this!

吃了这个开源小玩意让单片机开发和Arduino一样高效

Mysql database common sense storage engine
随机推荐
Logstash ~ input of logstash (input)
做自媒体副业真能月入上万?这篇分享,不藏私
Do we media sidelines really earn tens of thousands a month? This article is shared without privacy
众昂矿业:萤石资源新的活跃应用领域
Part time jobs are higher than wages. In 2022, there are three sidelines with a monthly income of more than 10000
Finally complete the dream of student age - make a handheld machine and teach you with single chip microcomputer STM32
干货|app自动化测试之Appium 源码分析
看看项目经理是如何把一个项目带崩的
Logstash ~ filter of logstash
【acwing】1118. 分成互质组 ***(DFS)
离散数学 命题逻辑
numpy. Ndarray does not support logical operations
【论文精读】Perception-based seam cutting for image stitching
The conflict between Russia and Ukraine raised concerns. The five eye network security department suggested that allies strengthen the protection of key infrastructure
Weekly recommended short video: the competition between enterprises is to see who can seize the first opportunity
There is a prize for essay | a heavy prize of 5000 yuan. You are invited to contribute
Is it really safe to outsource model training? New research: outsourcers may implant backdoors to control bank lending
Build a short video platform and copy a demo of the login interface
[breakthrough in key and difficult points of C language] - dynamic memory management
终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你