Java基础——拆箱与装箱

1 包装类

Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而当要使用只针对对象设计的API或新特性(例如泛型),那么基本数据类型的数据就需要用包装类来包装。

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:6ba08ffe-4b0f-49f2-9b56-bba08bd493f0

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:3d8d8ed8-295c-4315-8f5e-e9086a3486d6

转为包装类的对象,是为了使用专门为对象设计的API和特性

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:27c8f620-e85f-4087-be2f-d72732c17f83

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:64022f40-2094-4c54-96c2-c1613a2dda38

转为基本数据类型,一般是因为需要运算,Java中的大多数运算符是为基本数据类型设计的。比较、算术等

基本数值—->包装对象

Integer obj1 = new Integer(4);

包装对象—->基本数值

Integer obj = new Integer(4);int num1 = obj.intValue();

JDK1.5之后,可以自动装箱与拆箱。

注意:只能与自己对应的类型之间才能实现自动装箱与拆箱。

Integer i = 4;
Integer i = 1;Double d = 1;

(1)把基本数据类型转为字符串

int a = 10;

(2)把字符串转为基本数据类型

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

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

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2c5b330e-1ca2-4372-bceb-acbb2694fe23

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:e62c083a-ecbf-476e-be7d-2f88d887b67f

  • public static Integer valueOf(String s):将字符串参数转换为对应的Integer包装类,然后可以自动拆箱为int基本类型
  • public static Long valueOf(String s):将字符串参数转换为对应的Long包装类,然后可以自动拆箱为long基本类型
  • public static Double valueOf(String s):将字符串参数转换为对应的Double包装类,然后可以自动拆箱为double基本类型

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

int a = Integer.parseInt("整数的字符串");double d = Double.parseDouble("小数的字符串");boolean b = Boolean.parseBoolean("true或false");​int a = Integer.valueOf("整数的字符串");double d = Double.valueOf("小数的字符串");boolean b = Boolean.valueOf("true或false");
Integer.MAX_VALUE和Integer.MIN_VALUELong.MAX_VALUE和Long.MIN_VALUEDouble.MAX_VALUE和Double.MIN_VALUE
Character.toUpperCase('x');Character.toLowerCase('X');
Integer.toBinaryString(int i) Integer.toHexString(int i)Integer.toOctalString(int i)
Double.compare(double d1, double d2)Integer.compare(int x, int y)
Integer a = 1;Integer b = 1;System.out.println(a == b);
Double d1 = 1.0;Double d2 = 1.0;System.out.println(d1==d2);
Integer i = 1000;double j = 1000;System.out.println(i==j);
Integer i = 1000;int j = 1000;System.out.println(i==j);
Integer i = 1;Double d = 1.0System.out.println(i==d);
public class TestExam {    public static void main(String[] args) {        int i = 1;        Integer j = new Integer(2);        Circle c = new Circle();        change(i,j,c);        System.out.println("i = " + i);

Original: https://www.cnblogs.com/CYan521/p/16397506.html
Author: 再美不及姑娘你
Title: Java基础——拆箱与装箱

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562348/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球