最常用和推荐的方法:Integer.parseInt()
这是最直接、最高效的方法,专门用于将 String 转换为基本数据类型 int。

语法
int number = Integer.parseInt(String s);
示例代码
public class StringToIntExample {
public static void main(String[] args) {
String strNumber = "123";
// 将 String 转换为 int
int number = Integer.parseInt(strNumber);
System.out.println("转换后的 int 值是: " + number);
System.out.println("类型: " + ((Object)number).getClass().getName()); // 输出 java.lang.Integer 的基本类型 int
// 可以进行数学运算
int result = number * 2;
System.out.println("运算结果: " + result);
}
}
异常处理(非常重要!)
String 不是有效的整数表示,parseInt() 会抛出 NumberFormatException 异常,在实际开发中,强烈建议使用 try-catch 语句来处理这种潜在的错误。
什么时候会抛出异常?
- 字符串包含非数字字符(
"12a3")。 - 字符串是空的()。
- 字符串是
null。 - 数字超出
int的范围(2147483648,因为int的最大值是2147483647)。
使用 try-catch 的示例:
public class SafeStringToInt {
public static void main(String[] args) {
String[] testStrings = {"123", "-456", "789.0", "abc", "", "2147483648", null};
for (String str : testStrings) {
try {
int number = Integer.parseInt(str);
System.out.println("成功转换: '" + str + "' -> " + number);
} catch (NumberFormatException e) {
// 捕获格式异常,但 str 为 null 时会抛出 NullPointerException
System.out.println("转换失败: '" + str + "' 不是有效的整数格式。");
} catch (NullPointerException e) {
// 单独处理 null 的情况
System.out.println("转换失败: 输入字符串为 null。");
}
}
}
}
输出:

成功转换: '123' -> 123
成功转换: '-456' -> -456
转换失败: '789.0' 不是有效的整数格式。
转换失败: 'abc' 不是有效的整数格式。
转换失败: '' 不是有效的整数格式。
转换失败: '2147483648' 不是有效的整数格式。
转换失败: 输入字符串为 null。
处理 null 和无效输入的更稳健方法:Integer.valueOf()
Integer.valueOf() 方法将 String 转换为 Integer 对象(int 的包装类),如果字符串无法解析,它同样会抛出 NumberFormatException。
parseInt() vs valueOf() 的核心区别:
| 方法 | 返回类型 | 描述 |
|---|---|---|
Integer.parseInt(String s) |
int (基本数据类型) |
直接返回一个 int 值,性能略高,不涉及对象创建。 |
Integer.valueOf(String s) |
Integer (对象) |
返回一个 Integer 对象,如果值在缓存范围内(-128 到 127),会返回缓存中的对象,否则创建新对象。 |
示例代码
public class ValueOfExample {
public static void main(String[] args) {
String strNumber = "200";
// 返回一个 Integer 对象
Integer integerObject = Integer.valueOf(strNumber);
// 自动拆箱 (Autounboxing):Integer 对象会自动转换为 int 基本类型
int number = integerObject;
System.out.println("转换后的 Integer 对象: " + integerObject);
System.out.println("对象类型: " + integerObject.getClass().getName());
System.out.println("自动拆箱后的 int 值: " + number);
}
}
何时使用 valueOf?
- 当你需要一个
Integer对象而不是int基本类型时,当你需要将int放入一个只接受对象的集合(如List<Integer>)中时。 - 在某些需要对象引用的场景下。
处理小数字符串(如 "123.45")的方法
如果你有一个包含小数点的字符串("123.45"),直接使用 Integer.parseInt() 会抛出异常,正确的做法是先将其转换为 double 或 float,然后进行类型转换(强制类型转换会截断小数部分,而不是四舍五入)。

步骤:
- 使用
Double.parseDouble()将字符串转为double。 - 使用强制类型转换
(int)将double转为int。
示例代码
public class StringWithDecimal {
public static void main(String[] args) {
String strNumber = "123.78";
try {
// 1. 先转换为 double
double doubleValue = Double.parseDouble(strNumber);
// 2. 再强制转换为 int (会截断小数部分)
int intValue = (int) doubleValue;
System.out.println("原始字符串: " + strNumber);
System.out.println("转为 double 后: " + doubleValue);
System.out.println("强制转为 int 后 (截断): " + intValue); // 输出 123
// 如果需要四舍五入,应该使用 Math.round()
long roundedLong = Math.round(doubleValue);
int roundedInt = (int) roundedLong;
System.out.println("使用 Math.round() 四舍五入后: " + roundedInt); // 输出 124
} catch (NumberFormatException e) {
System.out.println("转换失败: '" + strNumber + "' 不是有效的数字格式。");
}
}
}
总结与最佳实践
| 场景 | 推荐方法 | 说明 |
|---|---|---|
将 String 转换为基本类型 int |
Integer.parseInt() |
最常用,直接、高效。必须配合 try-catch 处理异常。 |
将 String 转换为 Integer 对象 |
Integer.valueOf() |
当需要一个对象时使用,同样必须配合 try-catch。 |
字符串可能为 null 或无效 |
try-catch 包裹 parseInt() 或 valueOf() |
健壮的代码必须考虑异常情况,这是处理转换失败的标准做法。 |
| 字符串包含小数点 | Double.parseDouble() + 强制转换 (int) 或 Math.round() |
注意强制转换是截断,不是四舍五入。 |
从 int 转换为 String |
String.valueOf(int i) 或 Integer.toString(int i) |
这是反向操作,也很有用。 |
核心要点:
- 优先使用
Integer.parseInt(),当你明确需要一个int基本类型时。 - 永远不要忘记异常处理,在不确定输入字符串是否合法时,务必用
try-catch包裹转换逻辑。 - 根据你的具体需求(需要基本类型还是对象)来选择
parseInt()或valueOf()。
