日期:2014-05-20  浏览次数:20746 次

高手能解释下:Integer.parseInt(s) 和 Integer.valueOf(s)的区别?
顶者有分!

------解决方案--------------------
static int parseInt(String s) 
将字符串参数作为有符号的十进制整数进行分析。
static Integer valueOf(String s) 
返回保持指定的 String 的值的 Integer 对象。
------解决方案--------------------
不会看源代码么
Java code

  public static Integer valueOf(String s) throws NumberFormatException {
    return new Integer(parseInt(s, 10));
  }

  public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
  }

------解决方案--------------------
没有区别,都可把字符串当作有符号的整数来分析
------解决方案--------------------
API 上这样描述的:
public static Integer valueOf(String s)
throws NumberFormatException返回保持指定的 String 的值的 Integer 对象。将该参数解释为表示一个有符号的十进制整数, 就好像将该参数赋予 parseInt(java.lang.String) 方法一样。结果是一个表示字符串指定的整数值的 Integer 对象。 
换句话说,该方法返回一个等于以下值的 Integer 对象: 

new Integer(Integer.parseInt(s))
------解决方案--------------------
探讨
不会看源代码么

Java code
public static Integer valueOf(String s) throws NumberFormatException {
return new Integer(parseInt(s, 10));
}

public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

------解决方案--------------------
学了了~

原来是返回类型不一样
------解决方案--------------------
用起来是没多大区别的
就像C#中的 int.parse(3); Convert.ToInt16(3);
------解决方案--------------------
探讨
引用:
不会看源代码么

Java code
public static Integer valueOf(String s) throws NumberFormatException {
return new Integer(parseInt(s, 10));
}

public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

Integer / int 那也是个区别-_-!

------解决方案--------------------
用起来是一样的。
------解决方案--------------------
jdk1.4中应用是有区别的,从jdk1.5以上版本用起来就没有区别了.
------解决方案--------------------
楼上说的对,自动装箱
------解决方案--------------------
探讨
static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行分析。
static Integer valueOf(String s)
返回保持指定的 String 的值的 Integer 对象。

------解决方案--------------------
一般用parseInt(String s)
------解决方案--------------------
探讨
引用:
不会看源代码么

Java code
public static Integer valueOf(String s) throws NumberFormatException {
return new Integer(parseInt(s, 10));
}

public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

Integer / int 那也是个区别-_-!

------解决方案--------------------
返回的类型不一样,一个是整数值(paseInt)(别一个是Integer对像(valueof),paseInt中参字符串中的字符都必须是十进制数字