273、Java基础49 - 数字与字符串【装箱和拆箱】 2019.11.20

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的子类
在这里插入图片描述

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);
          
    }
}

8、练习:装箱拆箱

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

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

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

// byte和Integer之间能否进行自动拆箱和自动装箱

public class IndexController {

    public static void main(String[] args) {
        byte b = 3;
        float f = 4;
        short s = 5;
        double d = 6;
         
        //自动装箱
        Byte bt = b;
        Float ft = f;
        Short st = s;
        Double db = d;
         
        //拆箱
        byte bt2 = bt;
        float ft2 = ft;
        short st2 = st;
        double db2 = db;
         
        System.out.println("结果:");
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Float.MAX_VALUE);
        System.out.println(Short.MAX_VALUE);
        System.out.println(Double.MAX_VALUE);
        
       // 127
	   // 3.4028235E38
	   // 32767
	   // 1.7976931348623157E308
    }
}

9、参考链接

[01] How2j - 数字与字符串系列教材 (一)- JAVA中基本类型的装箱和拆箱

发布了309 篇原创文章 · 获赞 229 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/youyouwuxin1234/article/details/103167520
今日推荐