【Java】 int与String类型间的相互转化
public class Test { public static void main(String[] args) { /* * int类型转String类型 */ int n1 = 9; //1.采用String类的valueOf方法 String s1=String.valueOf(n1); //2.采用+""的形式 String s2= n1+""; //3.Integer的toString方法 String s3= Integer.toString(n1); System.out.println("Number "+n1+" to String is:"+s1); System.out.println("Number "+n1+" to String is:"+s2); System.out.println("Number "+n1+" to String is:"+s3); /* * String类型转int类型 */ String s="123"; int i = Integer.parseInt( s ); System.out.println("String "+s+" to number is:"+i); } }
Number 9 to String is:9 Number 9 to String is:9 Number 9 to String is:9 String 123 to number is:123