Java中的装箱与拆箱是什么意思

发布时间:2021-07-24 11:54:21 作者:chen
来源:亿速云 阅读:227

这篇文章主要介绍“Java中的装箱与拆箱是什么意思”,在日常操作中,相信很多人在Java中的装箱与拆箱是什么意思问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中的装箱与拆箱是什么意思”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、Java数据类型

1、在说装箱与拆箱之前,先说一下Java的基本数据类型,Java从数据类型上可以划分为值类型与引用类型,值类型是四类八种,分别是:

数据类型内存默认值包装类
byte8位0Byte
short16位0short
int32位0Integer
long64位0L或0lLong
float32位0.0F或0.0fFloat
double64位0.0D或0.0dDouble
char16位\u0000Character
boolean8位flaseBoolean

2、引用类型:

Java中的装箱与拆箱是什么意思

3、值类型与引用类型的区别

 1.  从概念方面上来说:

 2.  从内存构建方面上来说:

 3.  从使用方面上来说:

二、Java数据类型转换

1、自动转换

  运算中,不同类型的数据先转化为同一类型,然后进行运算 

操作数1类型操作数2类型转换后的类型
byte、short、charintint
byte、short、char、intlonglong
byte、short、char、int、longfloatfloat
byte、short、char、int、long、floatdoubledouble

2、强制转换

int x;   double y;   x = (int)3.14 + (int)5.20  //精度丢失   y = (double)x + (double)8  //精度提升   输出:x = 8;y = 16.0

三、Java之装箱与拆箱

1、包装类

2、什么是装箱与拆箱

int x = 3;     Integer y = x;  //int --> Integer,Integer y = x <==> Integer y = Integer.valueOf(x)
Integer x = new Integer(5);     int y = x;  //Integer --> int,int y = x <==> int y = x.intValue()

3、装箱和拆箱是如何实现的

4、注意点:

  1. 鸿蒙官方战略合作共建——HarmonyOS技术社区

  2.  大量使用自动拆装箱会使性能降低,还会造成大量的内存消耗

  3.  在重载方法中,可能出现问题 

List<Integer> list = new ArrayList<>();    Integer x,y,z;    x = 1;y = 2;z = 4;    list.add(x);list.add(y);list.add(z);    list.remove(2);

Java中的装箱与拆箱是什么意思

在上面这段代码中ArrayList.remove方法有两个重载方法,那么list.remove(2)是调用了哪个方法,remove掉的是值为2的对象,还是remove了index为2,值为4的那个对象呢?

在这种情况下,编译器不会进行自动拆装箱,所以调用的是remove(int index),index为2值为4的这个Integer对象会被remove.

如果要调用 remove(Object o)的方法,应该这么写 list.remove(y)

  3.  缓存值问题

Integer i1 = 100;     Integer i2 = 100;     Integer i3 = 200;     Integer i4 = 200;     System.out.println(i1==i2);     System.out.println(i3==i4);     Output: true false

  Intteger.valueOf方法 

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

IntegerCache类 

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.                 }             }             hhigh = 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() {}     }

从源码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象

包装类常量池常量池范围
Byte存在[-128,127]
Short存在[-128,127]
Integer存在[-128,127]
Long存在[-128,127]
Character存在[0,127]
Float不存在
Double不存在

到此,关于“Java中的装箱与拆箱是什么意思”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. java中的自动装箱和拆箱是什么
  2. java中的装箱和拆箱是什么?怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:asp.net 反射的作用是什么

下一篇:vue项目打包后如何上传至GitHub并实现github-pages的预览

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》