File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Q25 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Q25" Watch "Marcus Green Q25" New topic

Marcus Green Q25

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 202
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?

Be Afraid...Be very Afraid...
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Apr 29, 2002
Posts: 202
Yeah youre right, im using jdk 1.5. Thanks! I didnt know that.
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
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 ]

SCWCD: Online Course, 50,000+ words and 200+ questions
subject: Marcus Green Q25
It's not a secret anymore!