Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green Q25

 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if you attempt to compile and run the following code?

Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);

1) 19 followed by 20
2) 19 followed by 11
3) Compile time error
4) 10 followed by 1

The exam says the answer is 3, I answered 2.

I ran the code and I found out answer 2 was the output. Anyone care to comment?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot compile that code with JDK 1.4. Because it will try to use an overloaded operator that sums two Integers, and that operator does not exists, nor the operator that sums a primitive interger with an Integer.

That's why. May you're compiling the code with JDK 1.5 and autoboxing is happening automatically.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah youre right, im using jdk 1.5. Thanks! I didnt know that.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very subtle change between the versions and it will affect many existing questions. I have spent a huge amount of time over the last few months working on JDK 1.5 mock exams and this includes compiling up code like this to see if effects like autoboxing change the result. In several cases the answer to the question changes.

(note these new questions are not available for consumption yet)
[ April 27, 2005: Message edited by: Marcus Green ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic