Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scjp1.5

 
Z Weitao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyClass221
{
public Integer startingI;
public void methodA()
{
Integer i=new Integer(25);
startingI=i;
methodB(i);
}
private void methodB(Integer i2)
{
i2=i2.intValue();
System.out.println(i2==startingI);//aa
System.out.println(i2.equals(startingI));

Integer ss=new Integer(8);
int s=ss.intValue();
System.out.println(s==ss);//bb
}
public static void main(String[] args)
{
new MyClass221().methodA();
}
}
请问为什么aa输出的是false,但是bb那里输出的是true?
希望赐教,不胜感激!
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


line 1: you are using "new", so the "ss" will not point to some constant literal in pool
line 2: here the value taken as int, and then boxed, i2 will point to constant literal in pool
aa: here you are comparing object explicitly created with new, and object created by autoboxing, with value within (-128,127), which is cached in constant pool
bb: here ss is unboxed before comparison, so you are comparing just two ints

Hint: next time, try to post your question in English :-)
提示: 下次, 尝试张贴您的问题用英语:-)
[ May 27, 2007: Message edited by: John Stone ]
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope Babelfish translated your question correctly...

Originally posted by SmileFish Fish:
System.out.println(i2==startingI);//aa

You are comparing two different Integer objects.
One (startingI) constructed as new Integer(25) the other boxed from an integer primitive (the primitive returned from Integer.intValue()), as in i2 = i2.intValue();

Originally posted by SmileFish Fish:
System.out.println(s==ss);//bb

You are comparing two integer primitives here.


Now, for an interesting variation try the following: change Integer i=new Integer(25) for Integer i=25;
Do you understand the result?

[ May 27, 2007: Message edited by: Sergio Tridente ]
[ May 27, 2007: Message edited by: Sergio Tridente ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "SmileFish Fish"-

Welcome to JavaRanch.

On your way in you may have missed that we have a JavaRanch Naming Policy for displayed (screen) names. Your displayed name must consist of a first name (or an initial), a space, and a family name (in that order) and not be obviously fictitious. Since yours "SmileFish Fish", does not conform with it, please take a moment to change it, which you can do right here.

Posters with nonconforming displayed names will be locked out of JavaRanch after a few posts using those names.

Thanks
-Barry

(NR) - search tag
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In future post in english please.
 
Z Weitao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,next time .I will use in english.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Z Weitao:
Thanks,next time .I will use in english.


Thankyou.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic