当前位置: 移动技术网 > IT编程>开发语言>Java > Java自学-数字与字符串 装箱和拆箱

Java自学-数字与字符串 装箱和拆箱

2019年09月27日  | 移动技术网IT编程  | 我要评论

java中基本类型的装箱和拆箱

步骤 1 : 封装类

所有的基本类型,都有对应的类类型
比如int对应的类是integer
这种类就叫做封装类

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        int i = 5;
         
        //把一个基本类型的变量,转换为integer对象
        integer it = new integer(i);
        //把一个integer对象,转换为一个基本类型的int
        int i2 = it.intvalue();
         
    }
}

步骤 2 : number类

数字封装类有
byte,short,integer,long,float,double
这些类都是抽象类number的子类
number类

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        int i = 5;
         
        integer it = new integer(i);
        //integer是number的子类,所以打印true
        system.out.println(it instanceof number);
    }
}

步骤 3 : 基本类型转封装类

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        integer it = new integer(i);
         
    }
}

步骤 4 : 封装类转基本类型

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        integer it = new integer(i);
         
        //封装类型转换成基本类型
        int i2 = it.intvalue();
         
    }
}

步骤 5 : 自动装箱

不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        integer it = new integer(i);
         
        //自动转换就叫装箱
        integer it2 = i;
         
    }
}

步骤 6 : 自动拆箱

不需要调用integer的intvalue方法,通过=就自动转换成int类型,就叫拆箱

package digit;
  
public class testnumber {
  
    public static void main(string[] args) {
        int i = 5;
  
        integer it = new integer(i);
          
        //封装类型转换成基本类型
        int i2 = it.intvalue();
         
        //自动转换就叫拆箱
        int i3 = it;
          
    }
}

步骤 7 : int的最大值,最小值

int的最大值可以通过其对应的封装类integer.max_value获取

package digit;
  
public class testnumber {
  
    public static void main(string[] args) {
 
        //int的最大值
        system.out.println(integer.max_value);
        //int的最小值      
        system.out.println(integer.min_value);
          
    }
}

练习

  1. 对byte,short,float,double进行自动拆箱和自动装箱

  2. byte和integer之间能否进行自动拆箱和自动装箱

  3. 通过byte获取byte的最大值

答案

package digit;
 
public class testnumber {
 
    public static void main(string[] args) {
        // 1. 对byte,short,float,double进行自动拆箱和自动装箱
        byte b = 1;
        short s = 2;
        float f = 3.14f;
        double d = 6.18;
 
        // 自动装箱
        byte b1 = b;
        short s1 = s;
        float f1 = f;
        double d1 = d;
        // 自动拆箱
        b = b1;
        s = s1;
        f = f1;
        d = d1;
 
        // 2. byte和integer之间能否进行自动拆箱和自动装箱
        integer i1 = b; //不能把byte直接自动装箱成integer
        b = new integer(1); //也不能把integer自动拆箱成 byte
         
        // 3. 通过byte获取byte的最大值
        system.out.println(byte.max_value);
         
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网