Integer.parseInt()和这个Integer.valueOf()有什么区别么?

public class Shiyan {

public static void main(String[] args) {

String chuan="82";
int zhuan=Integer.parseInt(chuan);
int zhuanyi=Integer.valueOf(chuan);
System.out.println(zhuan);
System.out.println(zhuanyi);
}

}
不知道两者有何区别??好疑惑啊!

static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行分析。

static Integer valueOf(int i)
返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s)
返回保持指定的 String 的值的 Integer 对象。

从返回值可以看出他们的区别 parseInt()返回的是基本类型int
而valueOf()返回的是包装类Integer Integer是可以使用对象方法的 而int类型就不能和Object类型进行互相转换

int zhuan=Integer.parseInt(chuan);
int zhuanyi=Integer.valueOf(chuan); 为什么你的程序返回值都可以用int来接收呢? 因为Integer和int可以自动转换
Integer i = 5; int k = i;像是这样表示是没有编译错误的
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-10-10
大家都说了是返回的类型不同.

但不赞同闫国上的的"较高的版本中:integer.valueof()也可以返回整形数值。 " 实际上integer.valueof()并不能返回int型.

在JDK1.5之后,也称JDK5.0之后,基本数据类型和其相应的封装类在运算与赋值时可以自动转换,但是在做为参数时不是会自动转换的.

你的代码中:

Integer.parseInt(chuan)返回值是int型的.
Integer.valueOf(chuan)返回值是Integer型的.把Integer赋值给int型的话,JRE会自己完成这些工作.

区别还是有的.如果你写一个方法的形参是int型的,比如:
void test(int a){
//todo:
};
当你调用这个方法的时候test(Integer.parseInt(chuan))会翻译通过,但test(Integer.valueOf(chuan))会翻译错误.
第2个回答  2008-10-09
在比较低的版本中:
integer.valueof()返回得是对象;
integer.parseint()返回的是一个整形数值。
在较高的版本中:
integer.valueof()也可以返回整形数值。
第3个回答  2008-10-09
Integer.valueof()返回的是Integer的对象。
Integer.parseInt() 返回的是一个int的值。
第4个回答  2008-10-10
Integer java.lang.Integer.valueOf(String s)
Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.

In other words, this method returns an Integer object equal to the value of:

new Integer(Integer.parseInt(s))
Parameters:
s the string to be parsed.
Returns:
an Integer object holding the value represented by the string argument.
Throws:
NumberFormatException if the string cannot be parsed as an integer.

=-================================
int java.lang.Integer.parseInt(String s)
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
s a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException if the string does not contain a parsable integer.
相似回答