aspose file tools*
The moose likes Cattle Drive and the fly likes Java Rule Roundup answer incorrect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Java Rule Roundup answer incorrect" Watch "Java Rule Roundup answer incorrect" New topic
Author

Java Rule Roundup answer incorrect

Steven McArdle
Greenhorn

Joined: Aug 12, 2009
Posts: 4
Hi,

I just looked at the Java Rule Roundup questions and at least one of your answers is incorrect (#16).

Integer a = new Integer(5);
Integer b = new Integer(5);

What is the result of running: if(a==b)

In this case the answer will in fact be true and NOT false.

The reason for this is that to save space the == operator with values of 127 or less will be equal.

If however, your question had been "What is the result of running: if(a != b)" then the answer would also be true. The 127 and less rule only applies to the ==.

Regards

Steven McArdle
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
You haven't actually tried running the code, have you?

The rule you're thinking of applied to autoboxing, and to values returned by the Integer.valueOf(int) method. It doesn't apply to values created by "new". "New" always creates a new instance.
Steven McArdle
Greenhorn

Joined: Aug 12, 2009
Posts: 4
Hi Mike,

Actually autoboxing has to play a part in this otherwise it wouldn't compile and you will find that for values of 127 and less, this particular test is an exception i.e. ERRRR what happened here??

So the answer is wrong and it will produce true in this case.

Regards

Steven McArdle
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
You haven't actually tried running the code, have you?
Steven McArdle
Greenhorn

Joined: Aug 12, 2009
Posts: 4
Hi Mike....

Sun Certified Programmer for Java 5 Study Guide Exam 310-055
Kathy Sierra & Bert Bates McGraw-Hill Osborne

Page 236 (Assignments)

"In order to save memory, two instances of the following wrapper objects will always be == when their primative values are the same
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127"

Therefore, I state again that the answer to the question is incorrect. The correct answer for two instances of Integer with a value in the range -128 to 127 (and the last time I looked 5 is definitely within this range) using the == operator will result in true and NOT false. ONLY the == operator is affected. This means that if(a != b) will also result in true for this range and thus for a proper test the != operator should be used.

Regards

Steven McArdle

Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Hi Steven,

What you are expecting to happen would happen for autoboxing.






Experience and talent are independent of age
Steven McArdle
Greenhorn

Joined: Aug 12, 2009
Posts: 4
I must apologise for miss-reading the question.

You are quite right. I didn't pay attention to the explicit creation of the Integer wrapper classes using new.

In the case of :

Integer i1 = 5;
Integer i2 = 5;

if(i1 == i2)
System.out.println("true");
else
System.out.println("false");

The output is true.

In the case of :

Integer i1 = new Integer(5);
Integer i2 = new Integer(5);

if(i1 == i2)
System.out.println("true");
else
System.out.println("false");

The output is false.


In the first case the compiler will point the second reference to the same instance when the range is between -128 and 127 to save space. But when
you create a new Instance using Integer i1 = new Integer(5) it will not.

I do think this would be a good addition to the test questions though.

Regards

Steven McArdle
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Rule Roundup answer incorrect
 
Similar Threads
Autoboxing / Unboxing doubt
== comparison
Wrappers and ==
Error in question at The JavaRanch Rules Round-up Game
how == works on Integer wrapper class