包装类

1、什么是包装类

所谓包装类,就是能够将简单类型表示为一个类,在执行变量类型的转换时,会大量使用包装类

image-20210903092840454

数值型包装类都继承至Number,而字符型和布尔型继承至Object。

2、包装类用途

  • 集合中不允许存放基本数据类型,使用包装类
  • 包含了每种基本类型的相关属性,如最大值、最小值、所占位数等。
  • 作为基本数据类型对应的类类型,提供一系列使用对象操作,如数据类型转换、进制转换等。

3、基本数据类型和包装类型的区别

1、在Java中,一切皆对象,但八大基本类型却不是对象。

2、声明方式的不同,基本类型无需通过new关键字来创建,而封装类型需new关键字。

3、存储方式及位置的不同,基本类型是直接存储变量的值保存在堆栈中能高效的存取,封装类型需要通过引用指向实例,具体的实例保存在堆中。
4、初始值的不同,封装类型的初始值为null,基本类型的的初始值视具体的类型而定,比如int类型的初始值为0,boolean类型为false;

5、使用方式的不同,比如与集合类合作使用时只能使用包装类型。

6、什么时候该用包装类,什么时候用基本类型,看基本的业务来定:这个字段允允许null值,就需要使用包装类型,如果不允许null值,,使用基本类型就可以了,用到比如泛型和反射调用函数,就需要用包装类!

public class WrapperTestTre {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Integer one=new Integer(100);
 8         Integer two=new Integer(100);
 9         //one和对two是两个不同的对象,==在比较对象时比较的是内存地址,两个是不同的空间,放的值相同
10         System.out.println("one==two:"+(one==two));
11         Integer three=100;//自动装箱
12         /*    Integer three=Integer.valueOf(100);
13          * 这时缓存区没有,就会构造一个
14          */
15         System.out.println("three==100:"+(three==100));//自动拆箱
16         Integer four=100;
17         /*实际执行的是    Integer four=Integer.valueOf(100); 这时缓存区有,就会直接取
18          * Java为了提高拆装箱效率,在执行过程中提供了一个缓存区(对象池)【类似于常量数组】,
19          * 如果传入的参数是,-128<参数<127会直接去缓存查找数据,如果有久直接产生,如果没有就隐式调用new方法产生
20          */
21         
22         System.out.println("three==four:"+(three==four));
23         Integer five=200;
24         System.out.println("five==200:"+(five==200));
25         Integer six=200;
26         //注:这里为200,超出了缓存区范围,所以都需要构建
27         System.out.println("five==six:"+(five==six));
28         /*
29          * 输出:
30          *     one==two:false
31             three==100:true
32             three==four:true
33             five==200:true
34             five==six:false
35 
36          */
37     }
38 
39 }

4、基本数据类型和包装类之间的转换

装箱:基本数据类型转换为包装类;

拆箱:包装类转换为基本数据类型。

public class WrapperTestOne {
 4     public static void main(String[] args){
 5         //1.自动装箱
 6         int t1=1;
 7         Integer t2=t1;
 8         //2.手动装箱
 9         Integer t3=new Integer(t1);
10         System.out.println("int类型t1="+t1);
11         System.out.println("自动装箱,Integer类型对象t2="+t2);
12         System.out.println("手动装箱,Integer类型t3="+t3);
13         
14         
15         //1.自动拆箱
16         int t4=t2;
17         //2.手动拆箱
18             //通过intValue()方法返回int值,还可以利用其他方法装换为其他类型
19         int t5=t2.intValue();
20         System.out.println("自动拆箱,int型t4="+t4);
21         System.out.println("手动拆箱,int型t5="+t5);
22     }
23 
24 }

5、基本数据类型和包装类的转换

通过包装类Integer.toString()将整型转换为字符串;

通过Integer.parseInt()将字符串转换为int类型;

通过valueOf()方法把字符串转换为包装类然后通过自动拆箱。

public class WrapperTestTwo {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         //基本数据类型转换为字符串
 8         int t1=12;
 9         String t2=Integer.toString(t1);
10         System.out.println("int转换为String:"+t2);
11         //字符串转换为基本数据类型
12         //通过paerInt方法
13         int t3=Integer.parseInt(t2);
14         //通过valeOf,先把字符串转换为包装类然后通过自动拆箱
15         int t4=Integer.valueOf(t2);
16         System.out.println("t3:"+t3);
17         System.out.println("t4:"+t4);
18 
19     }
20 
21 }

注:Java中除了float和double的其他基本数据类型,都有常量池

本文来自博客园,作者:寒露凝珠,转载请注明原文链接:https://www.cnblogs.com/china-soldier/p/15221790.html

原文地址:https://www.cnblogs.com/china-soldier/p/15221790.html