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

包装类详解

概述

Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:

基本类型对应的包装类(位于java.lang包中)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
Integer类
  • Integer类概述

    包装一个对象

方法名说明
public Integer(int value)根据 int 值创建 Integer 对象(过时)
public Integer(String s)根据 String 值创建 Integer 对象(过时)
public static Integer valueOf(int i)返回表示指定的 int 值的 Integer 实例
public static Integer valueOf(String s)返回保存指定String值的 Integer 对象
static string tobinarystring(int i)得到二进制
static string tooctalstring(int i)得到八进制
static string toHexstring(int i)得到十六进制
static int parseInt(string s)将字符串类型的整数转成int类型的整数
  • 示例代码

public class test14 {public static void main(String[] args) {//public Integer(int value):根据 int 值创建 Integer 对象(过时)Integer i1 = new Integer(100);System.out.println(i1);//100//public Integer(String s):根据 String 值创建 Integer 对象(过时)Integer i2 = new Integer("100");
//Integer i2 = new Integer("abc"); //NumberFormatExceptionSystem.out.println(i2);//100System.out.println("--------");//public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例Integer i3 = Integer.valueOf(100);//100System.out.println(i3);//public static Integer valueOf(String s):返回保存指定String值的Integer对象Integer i4 = Integer.valueOf("100");//100System.out.println(i4);}
}
装箱与拆箱

基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:

  • 装箱:从基本类型转换为对应的包装类对象。

  • 拆箱:从包装类对象转换为对应的基本类型。

用Integer与 int为例:

基本数值---->包装对象

Integer i = new Integer(4);//使用构造函数函数
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法

包装对象---->基本数值

自动装箱与自动拆箱

由于我们经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。例如:

基本类型与字符串之间的转换

基本类型转换为String

  • 转换方式

  • 方式一:直接在数字后加一个空字符串

  • 方式二:通过String类静态方法valueOf()

  • 示例代码

public class IntegerDemo {public static void main(String[] args) {//int --- Stringint number = 100;//方式1String s1 = number + "";System.out.println(s1);//方式2//public static String valueOf(int i)String s2 = String.valueOf(number);System.out.println(s2);System.out.println("--------");}
}

String转换成基本类型

除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:

  • public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。

  • public static short parseShort(String s):将字符串参数转换为对应的short基本类型。

  • public static int parseInt(String s):将字符串参数转换为对应的int基本类型。

  • public static long parseLong(String s):将字符串参数转换为对应的long基本类型。

  • public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。

  • public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。

  • public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。

代码使用(仅以Integer类的静态方法parseXxx为例)如:

  • 转换方式

    • 方式一:先将字符串数字转成Integer,再调用valueOf()方法

    • 方式二:通过Integer静态方法parseInt()进行转换

  • 示例代码

注意:如果字符串参数的内容无法正确转换为对应的基本类型,则会抛出java.lang.NumberFormatException异常。

底层原理

建议:获取Integer对象的时候不要自己new,而是采取直接赋值或者静态方法valueOf的方式

因为在实际开发中,-128~127之间的数据,用的比较多。如果每次使用都是new对象,那么太浪费内存了。

所以,提前把这个范围之内的每一个数据都创建好对象,如果要用到了不会创建新的,而是返回已经创建好的对象。

//1.利用构造方法获取Integer的对象(JDK5以前的方式)
/*Integer i1 = new Integer(1);Integer i2 = new Integer("1");System.out.println(i1);System.out.println(i2);*///2.利用静态方法获取Integer的对象(JDK5以前的方式)
Integer i3 = Integer.valueOf(123);
Integer i4 = Integer.valueOf("123");
Integer i5 = Integer.valueOf("123", 8);System.out.println(i3);
System.out.println(i4);
System.out.println(i5);//3.这两种方式获取对象的区别(掌握)
//底层原理:
//因为在实际开发中,-128~127之间的数据,用的比较多。
//如果每次使用都是new对象,那么太浪费内存了
//所以,提前把这个范围之内的每一个数据都创建好对象
//如果要用到了不会创建新的,而是返回已经创建好的对象。
Integer i6 = Integer.valueOf(127);
Integer i7 = Integer.valueOf(127);
System.out.println(i6 == i7);//trueInteger i8 = Integer.valueOf(128);
Integer i9 = Integer.valueOf(128);
System.out.println(i8 == i9);//false//因为看到了new关键字,在Java中,每一次new都是创建了一个新的对象
//所以下面的两个对象都是new出来,地址值不一样。
/*Integer i10 = new Integer(127);Integer i11 = new Integer(127);System.out.println(i10 == i11);Integer i12 = new Integer(128);Integer i13 = new Integer(128);System.out.println(i12 == i13);*/

http://www.lryc.cn/news/306815.html

相关文章:

  • vue3与vue2的区别
  • SSL OV证书和DV、EV证书的区别
  • 一款.NET下 WPF UI框架介绍
  • 东莞IBM服务器维修之IBM x3630 M4阵列恢复
  • Flask基础学习4
  • mac安装zookeeper
  • IT资讯——全速推进“AI+鸿蒙”战略布局!
  • 数据结构知识点总结-线性表(3)-双向链表定义、循环单链表、、循环双向链表、静态链表、顺序表与链表的比较
  • JAVA学习-控制执行流程.for
  • 面试总结之JVM入门
  • 适配器模式(Adapter Pattern) C++
  • 【程序员英语】【美语从头学】初级篇(入门)(笔记)Lesson 16 At the Shoe Store 在鞋店
  • 嵌入式系统在物联网中的应用与发展趋势
  • BTC网络 vs ETH网络
  • Android 开发一个耳返程序(录音,实时播放)
  • 提高办公效率:Excel在文秘与行政办公中的应用技巧
  • Object.groupBy分组方法
  • 从初步的需求收集到详细的规划和评估
  • 石灰窑工艺流程以及富氧低氧燃烧技术
  • LeetCode 2960.统计已测试设备
  • vue中component is和keepAlive组合使用
  • 使用 Koltin 集合时容易产生的 bug 注意事项
  • CKA认证,开启您的云原生之旅!
  • 基于springboot+vue的抗疫物资管理系统(前后端分离)
  • nebula容器方式安装:docker 安装nebula到windows
  • 干洗行业上门预约解决方案,干洗店洗鞋店小程序开发;
  • 【Spring Boot 3】【JPA】@ManyToOne 实现一对多单向关联
  • Mathematica学习笔记收纳
  • java反射高级用列(脱敏+aop)
  • C++函数对象包装器function类详解