Java如何将字符串String转换为整型Int
作者:码说TM 时间:2023-11-10 21:40:19
用法
在java中经常会遇到需要对数据进行类型转换的场景,String类型的数据转为Int类型属于比较常见的场景,主要有两种转换方法:
1. 使用Integer.parseInt(String)方法
2. 使用Integer.valueOf(String)方法
具体demo如下:
public void convert() {
// 1.使用Integer.parseInt(String)
String str1 = "31";
Integer num1 = Integer.parseInt(str1);
System.out.print("字符串31转换为数字:");
System.out.println(num1);
// 2.使用Integer.valueOf(String)
String str2 = "32";
Integer num2 = Integer.valueOf(str2);
System.out.print("字符串32转换为数字:");
System.out.println(num2);
}
执行结果:
根据执行结果可见,两种方式都能完成字符串到整型的转换。
注意点
但需要注意的是,使用这两种方法都有一个前提,那就是待转换字符串的内容必须为纯数字。
不难发现上面demo中的待转换字符串都是"31"、"32"这种由纯数字组成的字符串,如果待转字符串中出现了除数字以外的其他字符,则程序会抛出异常。
如下demo所示,在字符串中加入小写英文字母,并用try-catch语句包裹代码段以捕捉会出现的异常。(因为我们已经知道,带字母的字符串转换为整型会出现数字格式转换的异常,所以选择catch NumberFormatException)
public void convert() {
// 1.Integer.parseInt(String)
try {
String str1 = "31a";
Integer num1 = Integer.parseInt(str1);
System.out.print("字符串31a转换为数字:");
System.out.println(num1);
} catch (NumberFormatException e) {
System.out.println("Integer.parseInt(String)方法执行异常");
e.printStackTrace();
}
// 1.Integer.valueOf(String)
try {
String str2 = "32b";
Integer num2 = Integer.valueOf(str2);
System.out.print("字符串32b转换为数字:");
System.out.println(num2);
} catch (NumberFormatException e) {
System.out.println("Integer.valueOf(String)方法执行异常");
e.printStackTrace();
}
}
从执行结果可见,这段代码分别在Integer.parseInt(String)方法和Integer.valueOf(String)位置触发了NumberFormatException,其原因都是被转换的字符串中存在英文字母,无法转换成整型。
性能比较
我们可以通过使用System.nanoTime()来查看两种方法执行的时间差
public static void convert() {
// 1.Integer.parseInt(String)
String str1 = "321";
long before1 = System.nanoTime();
Integer.parseInt(str1);
long interval1 = System.nanoTime() - before1;
System.out.print("Integer.parseInt(String)的执行时长(纳秒):");
System.out.println(interval1);
// 1.Integer.valueOf(String)
String str2 = "332";
long before2 = System.nanoTime();
Integer.valueOf(str2);
long interval2 = System.nanoTime() - before2;
System.out.print("Integer.valueOf(String)的执行时长(纳秒):");
System.out.println(interval2);
}
其中,interval1和interval2的值分别指两个方法的执行前后系统时间之差,单位是纳秒,执行多次后均可发现,Integer.valueOf(String)方法的执行时长要小于Integer.parseInt(String)方法,性能更优
来源:https://blog.csdn.net/qq_33323054/article/details/126256907
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Object类中的常用API介绍
android使用DataBinding来设置空状态
![](https://img.aspxhome.com/file/2023/4/88344_0s.gif)
Java远程连接Linux服务器并执行命令及上传文件功能
Java map 优雅的元素遍历方式说明
java实现科研信息管理系统
![](https://img.aspxhome.com/file/2023/9/67009_0s.jpg)
PageHelper在springboot+mybatis框架中的使用步骤及原理解析
![](https://img.aspxhome.com/file/2023/4/101984_0s.png)
Flutter中嵌入Android 原生TextView实例教程
![](https://img.aspxhome.com/file/2023/5/83515_0s.png)
百度人脸识别之人脸识别FaceIdentify(签到考勤)
![](https://img.aspxhome.com/file/2023/2/126032_0s.jpg)
VMware虚拟机下hadoop1.x的安装方法
SpringBoot项目创建使用+配置文件+日志文件详解
![](https://img.aspxhome.com/file/2023/9/59509_0s.jpg)
java自带的MessageDigest实现文本的md5加密算法
简单的一次springMVC路由跳转实现
![](https://img.aspxhome.com/file/2023/6/110896_0s.png)
Java两种常用的随机数生成方式(小白总结)
![](https://img.aspxhome.com/file/2023/9/108399_0s.png)
解决Jackson解析嵌套类问题(MismatchedInputException)
![](https://img.aspxhome.com/file/2023/9/57909_0s.png)
C#实现Word转换TXT的方法详解
![](https://img.aspxhome.com/file/2023/4/85714_0s.png)
SpringBoot通过JSON传递请求参数的实例详解
![](https://img.aspxhome.com/file/2023/5/128835_0s.png)
Android APK反编译图文教程
![](https://img.aspxhome.com/file/2023/9/139059_0s.jpg)
Java RabbitMQ高级特性详细分析
![](https://img.aspxhome.com/file/2023/6/116136_0s.png)
C# 禁用鼠标中间键的方法
C#获取所有进程的方法
![](https://img.aspxhome.com/file/2023/8/108808_0s.png)