数字与字符串相互转换

时间:2020-02-01 00:53:07   收藏:0   阅读:103

数字转字符串

方法一:使用String类的静态方法valueOf()

方法二:先把基本类型装箱为对象,然后调用对象的toString方法

                   

 1 public class TestNumber {
 2   
 3     public static void main(String[] args) {
 4         int i = 5;
 5          
 6         //方法1
 7         String str = String.valueOf(i);
 8          
 9         //方法2
10         Integer it = i;
11         String str2 = it.toString();
12          
13     }
14 }

字符串转数字

调用Integer的静态方法parseInt

 1 public class TestNumber {
 2   
 3     public static void main(String[] args) {
 4  
 5         String str = "999";
 6          
 7         int i= Integer.parseInt(str);
 8          
 9         System.out.println(i);
10          
11     }
12 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!