java中的String类与Integer包装类的转换

本文主要讲述java中的String类与Integer类的相互转化

示例代码如下:

 1 public class WrapperInteger {
 2     public static void main(String[] args) {
 3         Integer i = 10;
 4         // 将 Integer -> String
 5         // 方式1:
 6         String s1 = String.valueOf(i);
 7         System.out.println("s: "+s1);
 8         // 方式2:
 9         String s2 = i + "";
10         System.out.println("s: "+s2);
11         // 方式3:
12         String s3 = i.toString();
13         System.out.println("s: "+s3);
14 
15         // 将String -> Integer
16         String str = "123";
17         // 方式1:
18         int num = Integer.parseInt(str);
19         System.out.println(num);
20         // 方式2:
21         Integer integer = new Integer(str);// 自动装箱
22         System.out.println(integer);
23     }
24 }

 

posted @ 2022-12-26 20:41  zwGitOne  阅读(44)  评论(0编辑  收藏  举报