基本数据与封装对象转换(Java)

文章详细介绍了Java中的基本数据类型,包括boolean、byte、char等,并展示了如何将这些基本数据类型转换为对应的封装对象,如Boolean、Byte等,同时也演示了封装对象如何转换回基本数据类型。此外,还讨论了借助String进行转换的方法以及Java中的自动转换规则,特别是低精度到高精度的自动转换和高精度到低精度需要强制转换的原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 基本数据与对象封装转换

1.1 8种基本数据类型:

boolean(true/false),byte(1 Byte),char(2 Byte),short(2 Byte),int(4 Byte),float(4 Byte),long(8 Byte),double(8 Byte)

1.2 基本数据类型 – > 封装对象:

xxxx.ValueOf() // xxxx为对应的封装类
package com;

public class Main {
	public static void main(String[] args) throws Exception {
		boolean a = true;
		System.out.println(Boolean.valueOf(a).getClass().getName());
		byte b = 0;
		System.out.println(Byte.valueOf(b).getClass().getName());
		char c = '0';
		System.out.println(Character.valueOf(c).getClass().getName());
		short d = 0;
		System.out.println(Short.valueOf(d).getClass().getName());
		int e = 0; // 整型默认 int
		System.out.println(Integer.valueOf(e).getClass().getName());
		float f = 0.0f; // 浮点数默认 double(需要f声明为float)
		System.out.println(Float.valueOf(f).getClass().getName());
		long g = 0l;
		System.out.println(Long.valueOf(g).getClass().getName());
		double h = 0.0d;
		System.out.println(Double.valueOf(h).getClass().getName());
	}
}

在这里插入图片描述

1.3 封装对象 – > 基本数据类型:

xxxx.xxxValue() xxxx // 为对应的对象 
package com;

public class Main {
	public static void main(String[] args) throws Exception {
		Boolean a = true;
		System.out.println(a.booleanValue());
		Byte b = 0;
		System.out.println(b.byteValue());
		Character c = '0';
		System.out.println(c.charValue());
		Short d = 0;
		System.out.println(d.shortValue());
		Integer e = 0; // 整型默认 int
		System.out.println(e.intValue());
		Float f = 0.0f; // 浮点数默认 double(需要f声明)
		System.out.println(f.floatValue());
		Long g = 0l;
		System.out.println(g.longValue());
		Double h = 0.0d;
		System.out.println(h.doubleValue());
	}
}

在这里插入图片描述

1.4 借助String类型作为中间桥梁

xxx.valueOf // String --> 基本数据封装对象
package com;

public class Main {
	public static void main(String[] args) throws Exception {
		System.out.println(Boolean.valueOf("false"));
		System.out.println(Byte.valueOf("0"));
		System.out.println(Character.valueOf('0')); // 这里需要使用 char
		System.out.println(Short.valueOf("0"));
		System.out.println(Integer.valueOf("0"));
		System.out.println(Float.valueOf("0.0"));
		System.out.println(Long.valueOf("0"));
		System.out.println(Double.valueOf("0.0"));
	}
}

在这里插入图片描述

xxx.toString() // 基本封装对象 --> String
public class Main {
	public static void main(String[] args) throws Exception {
		Boolean a = true;
		System.out.println(a.toString());
		Byte b = 0;
		System.out.println(b.toString());
		Character c = '0';
		System.out.println(c.toString());
		Short d = 0;
		System.out.println(d.toString());
		Integer e = 0; // 整型默认 int
		System.out.println(e.toString());
		Float f = 0.0f; // 浮点数默认 double(需要f声明)
		System.out.println(f.toString());
		Long g = 0l;
		System.out.println(g.toString());
		Double h = 0.0d;
		System.out.println(h.toString());
	}
}

在这里插入图片描述

2. 自动转换规则

  低精度向高精度可以自动默认转换,高精度向低精度转换需要强制转换

范围小的类型向范围大的类型提升, 
byte、short、char 运算时直接提升为 int 。
byte、short、char‐‐>int‐‐>long‐‐>float‐‐>double
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

psudd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值