Java开发中数据类型转换是高频操作,尤其是Integer、Double这类数值类型,经常需要和字符串、其他数值类型互相转换,原生类库和第三方工具都能提供对应的实现方案。

Java原生转换方法
Integer类型转换
Integer类本身提供了多种转换方法,覆盖字符串转Integer、Integer转其他类型等常见场景。
字符串转Integer可以使用parseInt方法,该方法会抛出NumberFormatException异常,需要做好异常处理:
public class IntegerConvertDemo {
public static void main(String[] args) {
String numStr = "123";
try {
// 字符串转Integer
Integer num = Integer.parseInt(numStr);
System.out.println("转换后的Integer值:" + num);
// Integer转int
int intValue = num.intValue();
System.out.println("Integer转int结果:" + intValue);
// Integer转double
double doubleValue = num.doubleValue();
System.out.println("Integer转double结果:" + doubleValue);
} catch (NumberFormatException e) {
System.out.println("字符串格式不正确,无法转换为Integer");
}
}
}
Double类型转换
Double类的转换逻辑和Integer类似,同样提供了parseDouble方法处理字符串转Double的场景:
public class DoubleConvertDemo {
public static void main(String[] args) {
String doubleStr = "123.45";
try {
// 字符串转Double
Double doubleNum = Double.parseDouble(doubleStr);
System.out.println("转换后的Double值:" + doubleNum);
// Double转double
double d = doubleNum.doubleValue();
System.out.println("Double转double结果:" + d);
// Double转Integer(会丢失小数部分)
Integer intNum = doubleNum.intValue();
System.out.println("Double转Integer结果:" + intNum);
} catch (NumberFormatException e) {
System.out.println("字符串格式不正确,无法转换为Double");
}
}
}
常用第三方转换类库
Apache Commons Lang
Apache Commons Lang是Java开发中常用的工具类库,其中的NumberUtils类提供了更安全的数值转换方法,默认返回默认值而不是抛出异常:
import org.apache.commons.lang3.math.NumberUtils;
public class CommonsLangConvertDemo {
public static void main(String[] args) {
String validStr = "123";
String invalidStr = "abc";
// 字符串转Integer,转换失败返回默认值0
Integer num1 = NumberUtils.toInt(validStr, 0);
Integer num2 = NumberUtils.toInt(invalidStr, 0);
System.out.println("有效字符串转换结果:" + num1);
System.out.println("无效字符串转换结果:" + num2);
// 字符串转Double,转换失败返回默认值0.0
Double double1 = NumberUtils.toDouble("123.45", 0.0);
Double double2 = NumberUtils.toDouble("xyz", 0.0);
System.out.println("有效字符串转Double结果:" + double1);
System.out.println("无效字符串转Double结果:" + double2);
}
}
Guava
Google的Guava工具库也提供了数据类型转换的相关工具,比如Ints、Doubles类可以处理数组转换、默认值返回等场景:
import com.google.common.primitives.Ints;
import com.google.common.primitives.Doubles;
public class GuavaConvertDemo {
public static void main(String[] args) {
String intStr = "456";
String doubleStr = "456.78";
// 字符串转int,转换失败返回默认值-1
int intResult = Ints.tryParse(intStr, 10, 0, intStr.length(), -1);
int invalidIntResult = Ints.tryParse("error", 10, 0, 5, -1);
System.out.println("Guava转换int结果:" + intResult);
System.out.println("Guava转换无效字符串int结果:" + invalidIntResult);
// 字符串转double,转换失败返回null
Double doubleResult = Doubles.tryParse(doubleStr);
Double invalidDoubleResult = Doubles.tryParse("error");
System.out.println("Guava转换double结果:" + doubleResult);
System.out.println("Guava转换无效字符串double结果:" + invalidDoubleResult);
}
}
转换注意事项
- 原生
parseInt、parseDouble方法在字符串格式不正确时会抛出NumberFormatException,使用时必须捕获异常或者提前校验字符串格式。 - Double转Integer时会直接截断小数部分,不会进行四舍五入,如果需要保留精度需要先处理小数部分。
- 第三方类库的默认值机制可以避免异常,但需要注意默认值是否符合业务场景,避免默认值掩盖真实的数据问题。
- 处理null值时要额外判断,避免空指针异常,比如先校验字符串是否为null再调用转换方法。
方案选择建议
如果项目已经引入了Apache Commons Lang或者Guava,优先使用第三方类库的方法,减少异常处理代码;如果是轻量项目不想引入额外依赖,使用原生方法并做好异常捕获即可。对于高频转换场景,可以封装统一的转换工具类,统一处理异常和默认值逻辑,提升代码可维护性。