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又装了下箱,返回IntegerType.There is another difference,
parseInt期待输入是String,而valueOfis notFeel the code:
See source code:
In summary, generally use the
Integer.parseInt(str),除非你要返回Integertype, otherwise there will be packaging and unboxing, which will consume some performance.parseint returns int directly. valueof will do the encapsulation