• 【Java】Integer包装类底层设计原理


    Java 中基本数据类型 和 包装类型的区别

    • 包装类是对象,拥有方法和字段,对象的调用都是通过引用对象的地址;基本类型不是
    • 声明方式不同:
      • 基本数据类型 不需要 new 关键字
      • 包装类型 需要 new 在 堆内存中进行new 来分配内存空间
    • 存储位置不同:
      • 基本数据类型 直接将值保存在值 栈中;
      • 包装类型 是把对象放在堆中,然后通过对象的引用来调用它们
    • 初始值不同:
      • int 初始值=0,boolean 初始值=false
      • 包装类型的初始值=null
    • 包装类型是引用的传递;基本类型是值传递

    示例代码

    1. package com.api.Demo07;
    2. public class Test18 {
    3. public static void main(String[] args) {
    4. /**
    5. * 包装类底层设计原理
    6. * int 与 Integer 区别
    7. * - int属于基本数据类型 值传递————Integer属于int包装 对象 类 引用类型
    8. * - 基本数据类型存放在栈空间中——局部变量表(方法)————包装类 属于对象 存放在 堆空间
    9. * - Integer属于包装类 默认值 null(包装类)————int基本数据类型 默认值 0
    10. * 实际开发中 使用 包装类 比较多
    11. */
    12. Integer integerA = 60;
    13. //底层帮我们 new Integer(); 赋值给integerA对象,然后给Integer 成员属性 value赋值
    14. /**
    15. * public static Integer valueOf(int i) {
    16. * if (i >= IntegerCache.low && i <= IntegerCache.high)
    17. * return IntegerCache.cache[i + (-IntegerCache.low)];
    18. * return new Integer(i);
    19. * }
    20. *
    21. * public Integer(int value) {
    22. * this.value = value;
    23. * }
    24. *
    25. * private final int value;
    26. */
    27. int intA = 60;
    28. System.out.println(integerA);//60
    29. /**
    30. * integer是一个对象,为什么直接可以System.out.println(integerA);输出呢,而不是对象的内存地址呢?
    31. * ∵ Integer(包装类)重写了toString(),输出对应的基本数据类型
    32. *
    33. * public void println(Object x) {
    34. * String s = String.valueOf(x);
    35. * synchronized (this) {
    36. * print(s);
    37. * newLine();
    38. * }
    39. * }
    40. *
    41. * public static String valueOf(Object obj) {
    42. * return (obj == null) ? "null" : obj.toString();
    43. * }
    44. *
    45. * public String toString() {
    46. * return toString(value);
    47. * }
    48. */
    49. }
    50. }

    1. package com.api.Demo07;
    2. public class Test19 {
    3. private Integer integerA;
    4. private Boolean aBoolean;
    5. private int intA;
    6. private byte byteA;
    7. private short shortA;
    8. private long longA;
    9. private char charA;
    10. private boolean booleanA;
    11. private float flaotA;
    12. private double doubleA;
    13. private String stringA;
    14. public static void main(String[] args) {
    15. new Test19().show();
    16. }
    17. public void show(){
    18. System.out.println(integerA);//null
    19. System.out.println(aBoolean);//null
    20. System.out.println(intA);//0
    21. System.out.println(byteA);//0
    22. System.out.println(shortA);//0
    23. System.out.println(longA);//0
    24. System.out.println(charA);// 没有输出任何值
    25. System.out.println(booleanA);//false
    26. System.out.println(flaotA);//0.0
    27. System.out.println(doubleA);//0.0
    28. System.out.println(stringA);//null
    29. }
    30. }

    下一篇文章:装箱和和拆箱设计原理

  • 相关阅读:
    java中关于文件读写的各种类及其作用
    这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你
    java计算机毕业设计线上医药用品分销系统设计与实现源码+系统+mysql数据库+lw文档+部署
    什么是短网址?如何调用接口生成短地址?
    Vue添加动态路由后,不同角色访问“/”产生404问题
    Word控件Spire.Doc 【图像形状】教程(4) 用 C# 中的文本替换 Word 中的图像
    机器学习文本分类
    06-JVM 性能调优
    linux apache安装及虚拟主机配置
    【go-zero】go 时间字符串转换为时间戳 MYSQL场景下 时区问题详解
  • 原文地址:https://blog.csdn.net/qq_57268251/article/details/133847364