*
* String和int的相互转换:
*
* String 到 int
* 1.因为要从String转为int,所以可以去int的包转类中去找一些方法:
* Integer-->构造方法--> intValue()-->int
* Integer-->public static int parseInt(String s)--int
* Integer-->public static Integer valueOf(String s)-->intValue()--int
* int 到 String
* Integer --> public static String toString(int i)--> String
* String --> public static String valueOf(int i) --> String
*/
public class Demo {
public static void main(String[] args) {
//String -- int
//1.
String str = "20";
int num = new Integer(str).intValue();
System.out.println("num = " + num);
//2.
int num2 = Integer.parseInt(str);
System.out.println("num2 = " + num2);
//3.
int num3 = Integer.valueOf(str).intValue();
System.out.println("num3 = " + num3);
//int -- String
int value = 1000;
String s = Integer.toString(value);
System.out.println("s = " + s);
String s2 = String.valueOf(value);
System.out.println("s2 = " + s2);
}
}
|
|