File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Rule Roundup answer incorrect

 
Steven McArdle
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't actually tried running the code, have you?
 
Steven McArdle
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steven,

What you are expecting to happen would happen for autoboxing.





 
Steven McArdle
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic