"景先生毕设|www.jxszl.com

java  int 和string相互转换

2023-09-12 15:40编辑: www.jxszl.com景先生毕设
               java  int 和string相互转换
int 转 String
int i=0;
String s="";
第一种方法:s=i+""; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
 
String 转  int
s="1";
int i;
第一种方法:i=Integer.parseInt(s); //直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛
异常,但会多产生一个对象

原文链接:http://www.jxszl.com/biancheng/JAVA/446509.html