高手能解释下:Integer.parseInt(s) 和 Integer.valueOf(s)的区别?

NickCheng 2009-02-20 09:38:39
顶者有分!
...全文
1963 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
aimsgmiss 2012-02-29
  • 打赏
  • 举报
回复
还是不太理解啊!迷茫
大仁 2011-06-15
  • 打赏
  • 举报
回复
受教了!
blueared4 2009-02-20
  • 打赏
  • 举报
回复
简单的说,Integer.parseInt(s)返回转换后的int基本类型,
Integer.valueOf(s)返回装换后的Integer类的对象,是类的对象。

不知道解释清楚没有啊,详细的请看:
http://blog.csdn.net/blueared4/archive/2009/02/20/3913017.aspx
amwip110 2009-02-20
  • 打赏
  • 举报
回复
返回的类型不一样,一个是整数值(paseInt)(别一个是Integer对像(valueof),paseInt中参字符串中的字符都必须是十进制数字
zfl110 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fulianglove 的回复:]
引用 2 楼 yywl62 的回复:
不会看源代码么

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 那也是个区别-_-!
[/Quote]
学习
csgdseed 2009-02-20
  • 打赏
  • 举报
回复
一般用parseInt(String s)
iwantnba 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luojihaidao 的回复:]
static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行分析。
static Integer valueOf(String s)
返回保持指定的 String 的值的 Integer 对象。
[/Quote]
ing
Daniel_Cao_ 2009-02-20
  • 打赏
  • 举报
回复
楼上说的对,自动装箱
wangpeng88888888 2009-02-20
  • 打赏
  • 举报
回复
jdk1.4中应用是有区别的,从jdk1.5以上版本用起来就没有区别了.
qgmzhfj 2009-02-20
  • 打赏
  • 举报
回复
用起来是一样的。
酒剑仙 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fulianglove 的回复:]
引用 2 楼 yywl62 的回复:
不会看源代码么

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 那也是个区别-_-!
[/Quote]
哦》
猿敲月下码 2009-02-20
  • 打赏
  • 举报
回复
用起来是没多大区别的
就像C#中的 int.parse(3); Convert.ToInt16(3);
mgoann 2009-02-20
  • 打赏
  • 举报
回复
学了了~

原来是返回类型不一样
fulianglove 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yywl62 的回复:]
不会看源代码么

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);
}
[/Quote]
Integer / int 那也是个区别-_-!
luojihaidao 2009-02-20
  • 打赏
  • 举报
回复
API 上这样描述的:
public static Integer valueOf(String s)
throws NumberFormatException返回保持指定的 String 的值的 Integer 对象。将该参数解释为表示一个有符号的十进制整数, 就好像将该参数赋予 parseInt(java.lang.String) 方法一样。结果是一个表示字符串指定的整数值的 Integer 对象。
换句话说,该方法返回一个等于以下值的 Integer 对象:

new Integer(Integer.parseInt(s))
fulianglove 2009-02-20
  • 打赏
  • 举报
回复
没有区别,都可把字符串当作有符号的整数来分析
kevin-yy 2009-02-20
  • 打赏
  • 举报
回复
不会看源代码么

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);
}
luojihaidao 2009-02-20
  • 打赏
  • 举报
回复
static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行分析。
static Integer valueOf(String s)
返回保持指定的 String 的值的 Integer 对象。
relucent 2009-02-20
  • 打赏
  • 举报
回复
Integer.parseInt(s) 返回的是基本数据类型 int
Integer.valueOf(s) 返回的是一个Integer

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧