----------------------------String--> % %----------------------------------------
把字符串转化为相应的数值:
int型 Integer.parseInt(字符串)
long型 Long.parseLong(字符串)
float型 Folat.valueOf(字符串).floatValue()
double型 Double.valueOf(字符串).doubleValue()
--------------------------String--> int------------------------------------------
1如何将字串 String 转换成整数 int?
String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常
第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),
也会抛异常,但会多产生一个对象
注: 字串转成 Double, Float, Long 的方法大同小异.
------------------------int --> String --------------------------------------------
2 如何将整数 int 转换成字串 String ?
int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
第一种方法:s=i+""; //会产生两个String对象
第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象
------------------------------String-->double:--------------------------------------
String-->double:
1:
double price =new Double(request.getParameter("p"));
2.
double price=Double.valueOf(request.getParameter("p"));
3.
double price=Double.parseDouble(request.getParameter("p"));
----------------------------double -->Stirng----------------------------------------
double -->Stirng
String p=String.valueOf(double);