关于Integer.valueOf?

owenzzh 2008-01-21 03:20:24
为什么
boolean a = (Integer.valueOf("1") == Integer.valueOf("1"));
boolean b = (Integer.valueOf("1") > Integer.valueOf("1"));
boolean c = (Integer.valueOf("1") < Integer.valueOf("1"));
System.out.println(a);
System.out.println(b);
System.out.println(c);


输出结果:
false
false
false

是Integer类型不能进行比较么?
...全文
971 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mohaiyong 2011-03-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 psyl 的回复:]
2楼说的对,生成新对象,所以是false.
[/Quote]

说的对·~
love1907 2008-01-22
  • 打赏
  • 举报
回复
哥哥,不是对象杂能用==比较呢,如果你确实要这样的话,哪就利用1.5以后的自动拆箱功能,如下

int a1 = (Integer.valueOf("1")
int a2 = Integer.valueOf("1"));
boolean a = a1==a2;

System.out.println(a);


注JDK1。5以后才能这样子,给分吧,
qixueqi 2008-01-21
  • 打赏
  • 举报
回复
产生新类
eques进行比较
psyl 2008-01-21
  • 打赏
  • 举报
回复
2楼说的对,生成新对象,所以是false.
sun_left 2008-01-21
  • 打赏
  • 举报
回复
Integer是封装类型,用操作符比较的是引用。
ChDw 2008-01-21
  • 打赏
  • 举报
回复
Integer.parse(str)中实际上是new Integer(parseInt(s, 10));

也就是总是返回一个新的Integer对象,所以a是false



Integer.valueOf("1") > Integer.valueOf("1")

实际上被编译器改成Integer.valueOf("1").intValue() > Integer.valueOf("1").intValue();
所以是false
CpRcF 2008-01-21
  • 打赏
  • 举报
回复
看看jdk的源代码就知道了
Integer.valueOf 生成了新的Integer对象
c2one 2008-01-21
  • 打赏
  • 举报
回复
Integer是一个对象,如果直接用==,<,>这些比较是符合对象比较法则
并不是值~`

62,623

社区成员

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

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