在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java自动装箱拆箱浅析

2017-2-7 13:41| 发布者: zhangjf| 查看: 458| 评论: 0

摘要: 自动装箱拆箱概念 在Java SE 5之后,为基本数据类型提供了自动装箱和拆箱功能,使得将基本类型转换与对象之间的转换变得极其便捷。| 基本数据类型 | 包装类 || int(4字节) | Integer || byte(1字节) ...
自动装箱拆箱概念
  1. 在Java SE 5之后,为基本数据类型提供了自动装箱和拆箱功能,使得将基本类型转换与对象之间的转换变得极其便捷。
  2. | 基本数据类型 | 包装类 |
  3. | int(4字节) | Integer |
  4. | byte(1字节) | Byte |
  5. | short(2字节) | Short |
  6. | long(8字节) | Long |
  7. | float(4字节) | Float |
  8. | double (8字节) | Double |
  9. | char(2字节) | Character |
  10. | boolean(未定) | Boolean |
  11. Integer i = 100;(拆箱)
  12. int j = i;(装箱)
复制代码
自动装箱拆箱的实现
  1. Integer i = 100;(拆箱)
  2. int j = i;(装箱)
  3. //上面两行代码的反编译如下:
  4. Integer i = Integer.valueOf(100);//拆箱
  5. int j = i.intValue();//装箱
  6. 对于其他基本类型的装箱和拆箱和int类似。
复制代码
案例分析 Integer
  1. (1)案例
  2. int a = 100;
  3. int b = 100;
  4. System.out.println(a == b);
  5. int c = 200;
  6. int d = 200;
  7. System.out.println(c == d);
  8. Integer e = new Integer(100);
  9. System.out.println(a == e);
  10. //结果
  11. true
  12. true
  13. true
  14. //反编译
  15. byte a = 100;
  16. byte b = 100;
  17. System.out.println(a == b);
  18. short c = 200;
  19. short d = 200;
  20. System.out.println(c == d);
  21. Integer e = new Integer(100);
  22. System.out.println(a == e.intValue());
  23. 如果改为下面的代码呢?
  24. (2)案例
  25. Integer a = 100;
  26. Integer b = 100;
  27. System.out.println(a == b);
  28. //int c = 200;
  29. Integer c = 200;
  30. Integer d = 200;
  31. System.out.println(c == d);//输出true
  32. Integer e = new Integer(100);
  33. System.out.println(a == e);
  34. //结果
  35. true
  36. false
  37. false
  38. //反编译结果
  39. Integer a = Integer.valueOf(100);
  40. Integer b = Integer.valueOf(100);
  41. System.out.println(a == b);
  42. Integer c = Integer.valueOf(200);
  43. Integer d = Integer.valueOf(200);
  44. System.out.println(c == d);
  45. Integer e = new Integer(100);
  46. System.out.println(a == e);
复制代码

(1)案例和(2)案例为啥不一样呢?

当 "=="运算符的两个操作数都是包装器类型的引用,则比较的是对象;而如果其中有一个操作数是表达式(即包含算术运算)或是基本类型则比较的是数值。

原因分析:我们查看一下valueOf()源码

  1. public static Integer valueOf(int i) {
  2. if (i >= IntegerCache.low && i <= IntegerCache.high)
  3. //这里有个IntegerCache
  4. return IntegerCache.cache[i + (-IntegerCache.low)];
  5. //当大于high小于low时才新建
  6. return new Integer(i);
  7. }
  8. //可以得知low=-128,high=127
  9. private static class IntegerCache {
  10. static final int low = -128;
  11. static final int high;
  12. static final Integer cache[];
  13. static {
  14. // high value may be configured by property
  15. int h = 127;
  16. String integerCacheHighPropValue =
  17. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  18. if (integerCacheHighPropValue != null) {
  19. try {
  20. int i = parseInt(integerCacheHighPropValue);
  21. i = Math.max(i, 127);
  22. // Maximum array size is Integer.MAX_VALUE
  23. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  24. } catch( NumberFormatException nfe) {
  25. // If the property cannot be parsed into an int, ignore it.
  26. }
  27. }
  28. high = h;
  29. cache = new Integer[(high - low) + 1];
  30. int j = low;
  31. for(int k = 0; k < cache.length; k++)
  32. cache[k] = new Integer(j++);
  33. // range [-128, 127] must be interned (JLS7 5.1.7)
  34. assert IntegerCache.high >= 127;
  35. }
  36. private IntegerCache() {}
  37. }
复制代码
Boolean
  1. boolean a = true;
  2. boolean b = true;
  3. System.out.println(a == b);
  4. Boolean c = true;
  5. Boolean d = true;
  6. System.out.println(c == d);
  7. //结果
  8. true
  9. true
  10. //反编译
  11. boolean a = true;
  12. boolean b = true;
  13. System.out.println(a == b);
  14. Boolean c = Boolean.valueOf(true);
  15. Boolean d = Boolean.valueOf(true);
  16. System.out.println(c == d);
复制代码

为什么是true?我们查看valueOf()源码

  1. public static Boolean valueOf(boolean b) {
  2. return (b ? TRUE : FALSE);
  3. }
  4. //注意TRUE和FALSE为常量
  5. public static final Boolean TRUE = new Boolean(true);
  6. public static final Boolean FALSE = new Boolean(false);
复制代码
Double
  1. double a = 1.0;
  2. double b = 1.0;
  3. System.out.println(a == b);
  4. Double c = 1.0;
  5. Double d = 1.0;
  6. System.out.println(c == d);
  7. //结果
  8. true
  9. false
  10. //反编译
  11. double a = 1.0D;
  12. double b = 1.0D;
  13. System.out.println(a == b);
  14. Double c = Double.valueOf(1.0D);
  15. Double d = Double.valueOf(1.0D);
  16. System.out.println(c == d);
  17. //valueOf()源码
  18. public static Double valueOf(double d) {//创建了新对象
  19. return new Double(d);
  20. }
复制代码
Long
  1. Integer a = 1;
  2. Integer b = 2;
  3. Integer c = 3;
  4. Integer d = 3;
  5. Integer e = 321;
  6. Integer f = 321;
  7. Long g = 3L;
  8. Long h = 2L;
  9. System.out.println(c==d);//-127~128
  10. System.out.println(e==f);//>128
  11. System.out.println(c==(a+b));//自动拆箱
  12. System.out.println(c.equals(a+b));//都是Integer类型
  13. System.out.println(g==(a+b));//自动拆箱,向上转型为long
  14. System.out.println(g.equals(a+b));//类型不匹配
  15. System.out.println(g.equals(a+h));//向上转型为Long
  16. //结果
  17. true
  18. false
  19. true
  20. true
  21. true
  22. false
  23. true
  24. //反编译
  25. Integer a = Integer.valueOf(1);
  26. Integer b = Integer.valueOf(2);
  27. Integer c = Integer.valueOf(3);
  28. Integer d = Integer.valueOf(3);
  29. Integer e = Integer.valueOf(321);
  30. Integer f = Integer.valueOf(321);
  31. Long g = Long.valueOf(3L);
  32. Long h = Long.valueOf(2L);
  33. System.out.println(c == d);
  34. System.out.println(e == f);
  35. System.out.println(c.intValue() == a.intValue() + b.intValue());
  36. System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
  37. System.out.println(g.longValue() == (long)(a.intValue() + b.intValue()));
  38. System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
  39. System.out.println(g.equals(Long.valueOf((long)a.intValue() + h.longValue())));
  40. //Long的equals
  41. public boolean equals(Object obj) {
  42. if (obj instanceof Long) {//类型不一样便是false
  43. return value == ((Long)obj).longValue();
  44. }
  45. return false;
  46. }
复制代码



来自: http://my.oschina.net/u/2361475/blog/599242

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 10:24

Copyright 2015-2025 djqfx

返回顶部