数字与字符串相互转换

数字转字符串

方法一:使用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 }

 

posted @ 2020-01-31 23:06  东功  阅读(734)  评论(0编辑  收藏  举报