This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scjp1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scjp1.5" Watch "scjp1.5" New topic
Author

scjp1.5

Z Weitao
Greenhorn

Joined: May 27, 2007
Posts: 2
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

Joined: May 04, 2007
Posts: 332


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

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In future post in english please.
Z Weitao
Greenhorn

Joined: May 27, 2007
Posts: 2
Thanks,next time .I will use in english.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Z Weitao:
Thanks,next time .I will use in english.


Thankyou.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scjp1.5