how2j.cn

本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



5分3秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器



步骤 1 : 封装类   
步骤 2 : Number类   
步骤 3 : 基本类型转封装类   
步骤 4 : 封装类转基本类型   
步骤 5 : 自动装箱   
步骤 6 : 自动拆箱   
步骤 7 : int的最大值,最小值   
步骤 8 : 练习-装箱拆箱   
步骤 9 : 答案-装箱拆箱   

所有的基本类型,都有对应的类类型
比如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(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
         
    }
}
数字封装类有
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); } }
1
2
3
4
5
6
7
8
9
10
11
12
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 :

基本类型转封装类

edit
package digit; public class TestNumber { public static void main(String[] args) { int i = 5; //基本类型转换成封装类型 Integer it = new Integer(i); } }
1
2
3
4
5
6
7
8
9
10
11
12
package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        Integer it = new Integer(i);
         
    }
}
步骤 4 :

封装类转基本类型

edit
package digit; public class TestNumber { public static void main(String[] args) { int i = 5; //基本类型转换成封装类型 Integer it = new Integer(i); //封装类型转换成基本类型 int i2 = it.intValue(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        Integer it = new Integer(i);
         
        //封装类型转换成基本类型
        int i2 = it.intValue();
         
    }
}
不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱
package digit; public class TestNumber { public static void main(String[] args) { int i = 5; //基本类型转换成封装类型 Integer it = new Integer(i); //自动转换就叫装箱 Integer it2 = i; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //基本类型转换成封装类型
        Integer it = new Integer(i);
         
        //自动转换就叫装箱
        Integer it2 = i;
         
    }
}
不需要调用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; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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的最大值,最小值

edit
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
2
3
4
5
6
7
8
9
10
11
12
13
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 :

练习-装箱拆箱

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
1. 对byte,short,float,double进行自动拆箱和自动装箱

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

3. 通过Byte获取byte的最大值
步骤 9 :

答案-装箱拆箱

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2024-02-09 感觉评论区有点不太对
wind_lz




我这个有一个不可转?报错了


2 个答案

木宇
答案时间:2024-05-05
打卡

Leslie_sakura
答案时间:2024-04-21



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢


2023-05-24 现在Eclipse11已经把这个转换方式删除掉了。
Shu_Enryu




第9行的Integer it = new Integer(i); 现在Eclipse11已经把这个转换方式删除掉了。 现在可以直接Integer a = i;


1 个答案

hncj指尖风雨
答案时间:2023-12-30
package demo2; public class Number { public static void main(String[] args) { byte b1 = 12; Byte B1 = b1; byte b2 = B1; System.out.println(Byte.MAX_VALUE); System.out.println(Byte.MIN_VALUE); System.out.println("---------------------"); short s1 = 4; Short S1 = s1; short s2 = S1; System.out.println(Short.MAX_VALUE); System.out.println(Short.MIN_VALUE); System.out.println("========================="); float f1 = 12.3F; Float F1 = f1; float f2 = F1; System.out.println(Float.MAX_VALUE); System.out.println(Float.MIN_VALUE); double d1 = 12.0000008; Double D1 = d1; double d2 = D1; System.out.println(Double.MAX_VALUE); System.out.println(Double.MIN_VALUE); } }



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢


2021-07-12 2021-7-12-AM-10-00
2021-05-05 数字与字符串-装箱拆箱 -练习
2021-03-08 装箱和拆箱的应用方面是什么


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 26 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢