Java的封装类和装箱拆箱详解

作者:才尽散人 时间:2023-09-20 22:41:22 

一、封装类

1.封装类概念

Java中存在基础数据类型,但是在某些情况下,我们要对基础数据类型进行对象的操作,例如,集合中只能存对象,而不能存在基础数据类型,于是便出现了封装类。封装类就是对基本数据类型进行封装,并用它生成对象,以便以对象方式操作基本数据类型。每一个基本数据类型都对应一种封装类。

2. 各个基础类型对应的封装类

基础类型封装类型
intInteger
byteByte
shortShort
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

二、装箱与拆箱

1.装箱与拆箱概念

  • 装箱:将基础数据类型自动转化为对应的封装类

  • 拆箱:将封装类自动转化为对应的基础数据类型

2.基础数据类型封装

public class Test {
   public static void main(String[] args) {
       int num = 1;
       Object obj = new Num(num);//父类型引用指向子类型对象
       System.out.println(obj);
   }
}
public class Num {
   int num;

public Num(int num) {
       this.num = num;
   }

public String toString() {
       return "" + num;
   }
}
//实现封装

3.自动装箱拆箱演示

public class Test {
   public static void main(String[] args) {
       int num = 10;

Integer num1 = num; // 自动装箱
       int num2 = num1; // 自动拆箱
   }
}

上面代码中,首先,num自动装箱为Integer类对象赋值给num1;然后,num1又自动拆箱为基本数据类型。

4.Integer中valueOf方法和 intValue方法源码

Integer在装箱过程中调用了Integer中的valueOf方法,拆箱时调用了Integer中的intValue方法。

valueOf方法:

public static Integer valueOf(int i) {
       if (i >= IntegerCache.low && i <= IntegerCache.high)
           return IntegerCache.cache[i + (-IntegerCache.low)];
       return new Integer(i);
   }

intValue方法:

public int intValue() {
       return value;
   }

三、自动装箱和拆箱中的一些问题

1.相同数值比较返回值为false

public class Main {
   public static void main(String[] args) {
       Integer num1 = 100;
       Integer num2 = 100;
       Integer num3 = 200;
       Integer num4 = 200;
       System.out.println(num1 == num2);
       System.out.println(num3 == num4);
   }
}
/*
* 运行结果
* true
* false
*/

源码:

private static class IntegerCache {
       static final int low = -128;
       static final int high;
       static final Integer cache[];

static {
           // high value may be configured by property
           int h = 127;
           String integerCacheHighPropValue =
               sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
           if (integerCacheHighPropValue != null) {
               try {
                   int i = parseInt(integerCacheHighPropValue);
                   i = Math.max(i, 127);
                   // Maximum array size is Integer.MAX_VALUE
                   h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
               } catch( NumberFormatException nfe) {
                   // If the property cannot be parsed into an int, ignore it.
               }
           }
           high = h;

cache = new Integer[(high - low) + 1];
           int j = low;
           for(int k = 0; k < cache.length; k++)
               cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7)
           assert IntegerCache.high >= 127;
       }

private IntegerCache() {}
   }

从源码中可以看出,在IntegerCache类中初始化了一个Integer数组,它的范围为-128到127。num1==num2在-128到127之间,因此给num1和num2赋值时,直接返回cache[ ]数组中的对象,属于同一个对象,返回值为true;而200超过了这个范围,给num3和num4赋值时,直接返回new Integer(),因此属于两个不同的对象,返回值false。

2.浮点型数值比较返回值为false7

public class Main {
   public static void main(String[] args) {
       Double num1 = 100.0;
       Double num2 = 100.0;
       Double num3 = 200.0;
       Double num4 = 200.0;
       System.out.println(num1 == num2);
       System.out.println(num3 == num4);
   }
}
/*
* 运行结果
* false
* false
*/

源码:

public static Double valueOf(double d) {
       return new Double(d);
   }

从源码中可以看出,Double中的valueOf()返回了一个新的封装类对象,因此都返回false。

来源:https://blog.csdn.net/kr20021127/article/details/130670997

标签:Java,封装类,装箱,拆箱
0
投稿

猜你喜欢

  • 详解 c# 克隆

    2021-12-16 16:23:12
  • Android中TextView显示插入的图片实现方法

    2023-08-06 00:27:42
  • Android App在线程中创建handler的方法讲解

    2021-06-24 17:00:33
  • SpringCloud Eureka服务注册中心应用入门详解

    2022-02-23 08:48:44
  • JAVA使用hutool工具实现查询树结构数据(省市区)

    2021-11-04 18:30:09
  • 关于线程池你不得不知道的一些设置

    2021-06-08 11:34:45
  • springboot做代理分发服务+代理鉴权的实现过程

    2021-06-28 03:22:14
  • 基于Spring Mvc实现的Excel文件上传下载示例

    2022-01-22 02:02:56
  • JAVA熔断和降级真实关系的图文详解

    2023-11-30 12:39:20
  • MyBatis Oracle 自增序列的实现方法

    2023-08-03 11:30:08
  • 解析:android 如何从JPEG生成BufferedImage

    2022-06-03 20:11:50
  • 详解Mybatis的二级缓存配置

    2023-03-20 10:48:37
  • C# List集合中获取重复值及集合运算详解

    2022-06-13 17:15:45
  • java多线程-读写锁原理

    2021-07-20 17:28:52
  • 如何在mapper文件中使用in("str1","str2")

    2023-07-10 10:40:51
  • Android编程之文件读写操作与技巧总结【经典收藏】

    2023-10-16 02:20:52
  • java与js代码互调示例代码

    2022-11-09 20:19:05
  • Java中GUI工具包AWT和Swing用法介绍

    2022-02-06 09:02:35
  • C# wpf 通过HwndHost渲染视频的实现方法

    2023-08-30 03:33:35
  • ANDROID中使用VIEWFLIPPER类实现屏幕切换(关于坐标轴的问题已补充更改)

    2021-09-05 10:51:29
  • asp之家 软件编程 m.aspxhome.com