您的位置:首页 > 产品设计 > UI/UE

intValue、parseInt、valueOf 方法区别及其使

2015-10-27 22:18 525 查看
intValue是把Integer对象类型变成int的数据类型;

parseInt是吧String 变成int的数据类型;

Valueof是吧给定的参数转化成对象类型;
// 第一个不常用
Integer a_=new Integer(123);    // a_是Integer类型

int a=a_.intValue();                    // 转化为int类型
System.out.println(a);

String b = "123";        // 一个string
int b_=Integer.parseInt(b);    // 把string转化成int
System.out.println(b_);

int c_=123;
String c=String.valueOf(c_);
System.out.println(c);
其中:由于valueOf方法是调用parseInt方法,并且由Integer.valueOf()方法来转换。所以parseInt稍微比valueOf快一点。         
public static Integer valueOf(String s, int radix) throws NumberFormatException {                  
return Integer.valueOf(parseInt(s,radix));        
}         
public static Integer valueOf(int i) {                         
if(i >= -128 && i <= IntegerCache.high)                                
return IntegerCache.cache[i + 128];                         
else                       
return new Integer(i);          
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: