JAVA中int、String的类型转换

来源:互联网 发布:bestie软件下载 编辑:程序博客网 时间:2024/04/30 01:36

int->String

int n= 12345;
String s= “”;
第一种:s += n; //会产生两个String对象
第二种:s += String.valueOf(n); //直接使用String类的静态方法,只产生一个对象

String->int

int n = 0;
Sting s = “123456”;
n = Integer.parseInt(s); //直接使用静态方法,不会产生多余的对象,但会抛出异常
n = Integer.valueOf(s).intValue(); //Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

字符串 String ->整数 int

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

整数 int ->字串 String

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = “” + i;

0 0