当前位置: 首页 > news >正文

包装类(基本数据类型对应的引用数据类型)

包装类(基本数据类型对应的引用数据类型)

Notice:

  • 基本数据类型存在栈里(基本类型基本没有属性和方法,所以基本类型的运算都靠Java提供的运算符)
  • 引用数据类型存在堆里
  • 所以栈里存的是堆里东西的地址

N

  • Object可统一所有数据,包装类的默认值是null
  • ​ byte Byte
  • ​ short Short
  • ​ int Integer
  • ​ long Long
  • ​ float Float
  • ​ double Double
  • ​ boolean Boolean
  • char Character

装箱,拆箱

1.装箱:把栈里的东西放到堆里面(基本类型转引用类型)

2.拆箱:把堆里的东西放到栈里面(引用类型转基本类型)

3.八种包装类提供不同类型间的转换方式:

  • Number父类中提供的六个共性方法(返回固定的基本类型,拆箱)

    • byteValue()
    • doubleValue()
    • floatValue()
    • intValue()
    • longValue()
    • shortValue()
  • Integer类提供的两个方法(装箱

    • Integer(int value)
    • Integer(String s)
    • valueOf(int i)
  • package 包装类;public class Apply {public static void main(String[] args) {//基本类型int num1=18;//使用Integer类创建对象Integer integer1=new Integer(num1);Integer integer2=Integer.valueOf(num1);System.out.println("装箱");System.out.println(integer1);System.out.println(integer2);//类型转换:拆箱,引用类型转成基本类型Integer integer3=new Integer(100);int num2=integer3.intValue();System.out.println("拆箱");System.out.println(num2);//jdk1.5之后,提供自动的装拆箱int age=30;//自动装箱Integer integer4=age;System.out.println("自动装箱");System.out.println(integer4);//自动拆箱int age2=integer4;System.out.println("自动拆箱");System.out.println(age2);}
    }
  • paeseXXX()静态方法

//1.基本类型转成字符串int n1=15;//1.1使用+号String s1=n1+"";//1.2使用Integer中的toString()方法String s2=Integer.toString(n1,16);System.out.println(s1);System.out.println(s2);//2.字符串转成基本类型String str="150";//使用Integer.parseInt(str)int n2=Integer.parseInt(str);System.out.println(n2);//3.boolean字符串形式转成基本类型String str2="true";boolean b1=Boolean.parseBoolean(str2);System.out.println(b1);
http://www.hskmm.com/?act=detail&tid=33250

相关文章:

  • luogu P7915 [CSP-S 2021] 回文
  • USACO 绿-蓝 思维题小记
  • Day16-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\classlei
  • 一个实用的短视频脚本创作指令分享
  • 字典树 Trie 乱讲
  • redis和mysql之间的数据一致性
  • ubuntu允许root登录桌面系统
  • 24. 两两交换链表中的节点
  • AI协科学家:技术革命还是安全噩梦?
  • 一个决定
  • 10月17日
  • npm镜像配置
  • 一些特性
  • ubuntu安装mysql
  • 计算机视觉技术与应用深度解析
  • AGC 板刷记录1
  • 2025.10.17总结
  • 记Windows 11环境Rust下载安装配置流程
  • K8s学习笔记(九) job与cronjob - 教程
  • [HZOI]CSP-S模拟33
  • [PaperReading] VLM2Vec-V2: Advancing Multimodal Embedding for Videos, Images, and Visual Documents
  • ShandongCCPC2024
  • 标悬浮展开多级菜单
  • Nimble:让SwiftObjective-C测试变得更优雅的匹配库 - 指南
  • 2025.10.17总结 - A
  • Ubuntu创建python桌面图标
  • 深入解析Pure恶意软件家族:从RAT到构建器再到开发者
  • Ubuntu上配置Flask应用程序的Nginx和uWSGI
  • 实验一 现代c++基础课程
  • 平均融资利率求法及ORACLE语法解析