public class IntegerDemo4 {
public static void main(String[] args) { String str = "123"; int i = Integer.parseInt(str);
// int i = Integer.valueOf(str);
//parseInt和valueOf在这里用结果都对,但区别是什么呢?谢谢大家解答一下^_^
System.out.println(i+1); double d = Double.parseDouble(str); System.out.println(d+1); }
}
valueOf
内部就用了parseInt
,区别在于parseInt
直接返回原始int
类型数据;而valueOf
又装了下箱,返回Integer
Type.There is another difference,
parseInt
期待输入是String
,而valueOf
is notFeel the code:
See source code:
In summary, generally use the
Integer.parseInt(str)
,除非你要返回Integer
type, otherwise there will be packaging and unboxing, which will consume some performance.parseint returns int directly. valueof will do the encapsulation