掌握自动转换与强制转换的原理与应用
类型转换是Java编程中的重要概念,它允许我们在不同数据类型之间进行转换。理解类型转换的机制对于编写正确和高效的Java程序至关重要。
箭头方向表示自动类型转换的方向(从小到大)
// 自动类型转换(向上转换)
byte b = 10;
short s = b; // byte → short
int i = s; // short → int
long l = i; // int → long
float f = l; // long → float
double d = f; // float → double
// 表达式中的自动提升
int x = 5;
float y = 3.2f;
double result = x + y; // int和float提升为double
// 强制类型转换(向下转换)
double d = 3.14159;
float f = (float) d; // double → float
long l = (long) f; // float → long
int i = (int) l; // long → int
short s = (short) i; // int → short
byte b = (byte) s; // short → byte
// 可能的精度损失
double pi = 3.14159;
int intPi = (int) pi; // 结果:3(小数部分丢失)
// 自动装箱(基本类型 → 包装类)
int primitive = 42;
Integer wrapper = primitive; // 自动装箱
// 自动拆箱(包装类 → 基本类型)
Integer wrapperInt = 100;
int primitiveInt = wrapperInt; // 自动拆箱
// 包装类之间的转换
Integer intValue = 123;
Double doubleValue = intValue.doubleValue();
String stringValue = intValue.toString();
// 其他类型转换为字符串
int num = 123;
String str1 = String.valueOf(num); // "123"
String str2 = Integer.toString(num); // "123"
String str3 = num + ""; // "123"
// 字符串转换为其他类型
String numStr = "456";
int intValue = Integer.parseInt(numStr);
double doubleValue = Double.parseDouble("3.14");
boolean boolValue = Boolean.parseBoolean("true");
源类型 | 目标类型 | 转换方式 | 是否安全 |
---|---|---|---|
byte | short, int, long, float, double | 自动转换 | ✅ 安全 |
short | int, long, float, double | 自动转换 | ✅ 安全 |
char | int, long, float, double | 自动转换 | ✅ 安全 |
int | long, float, double | 自动转换 | ⚠️ float可能精度损失 |
long | float, double | 自动转换 | ⚠️ float可能精度损失 |
float | double | 自动转换 | ✅ 安全 |
通过一个类型转换演示程序,我们来看看如何在实际项目中正确使用各种类型转换。
public class TypeConversionDemo {
public static void main(String[] args) {
// 1. 自动类型转换示例
System.out.println("=== 自动类型转换 ===");
byte byteValue = 10;
short shortValue = byteValue; // byte → short
int intValue = shortValue; // short → int
long longValue = intValue; // int → long
float floatValue = longValue; // long → float
double doubleValue = floatValue; // float → double
System.out.println("byte: " + byteValue);
System.out.println("转换后的double: " + doubleValue);
// 2. 强制类型转换示例
System.out.println("\n=== 强制类型转换 ===");
double pi = 3.14159;
int intPi = (int) pi; // 精度损失
float floatPi = (float) pi; // 轻微精度损失
System.out.println("原始值: " + pi);
System.out.println("转换为int: " + intPi);
System.out.println("转换为float: " + floatPi);
// 3. 包装类转换示例
System.out.println("\n=== 包装类转换 ===");
Integer wrapperInt = 100; // 自动装箱
int primitiveInt = wrapperInt; // 自动拆箱
// 包装类方法转换
String numberStr = wrapperInt.toString();
Double doubleFromInt = wrapperInt.doubleValue();
System.out.println("包装类转字符串: " + numberStr);
System.out.println("包装类转double: " + doubleFromInt);
// 4. 字符串转换示例
System.out.println("\n=== 字符串转换 ===");
String numStr = "123";
String floatStr = "45.67";
String boolStr = "true";
try {
int parsedInt = Integer.parseInt(numStr);
double parsedDouble = Double.parseDouble(floatStr);
boolean parsedBool = Boolean.parseBoolean(boolStr);
System.out.println("字符串转int: " + parsedInt);
System.out.println("字符串转double: " + parsedDouble);
System.out.println("字符串转boolean: " + parsedBool);
} catch (NumberFormatException e) {
System.out.println("数字格式错误: " + e.getMessage());
}
// 5. 表达式中的类型提升
System.out.println("\n=== 表达式类型提升 ===");
byte b1 = 10, b2 = 20;
// byte运算结果自动提升为int
int result = b1 + b2;
System.out.println("byte运算结果: " + result);
// 混合类型运算
int intVal = 10;
float floatVal = 3.5f;
double mixedResult = intVal * floatVal; // 提升为double
System.out.println("混合运算结果: " + mixedResult);
}
}
long bigNumber = 123456789012345L;
float floatNumber = bigNumber;
// 可能丢失精度int maxInt = Integer.MAX_VALUE;
byte smallByte = (byte) maxInt;
// 数据溢出String invalidNumber = "abc";
int number = Integer.parseInt(invalidNumber);
// NumberFormatException