aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Question 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 "Wrapper Question" Watch "Wrapper Question" New topic
Author

Wrapper Question

Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
On Marcus green exam1 Question 25 ask the following question:

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 answer given is 3.

This answer didn't seem correct to me so I typed in the program and complied it. The program ran and printed 19 followed by 11 answer 2. I'm I missing somthing about this question? Even if the wrapper class didn't know how to convert the objects and add them would it not use the toString() method and and print the string for both object since it is in a print statement?
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Which jdk version are you using ?
For jdk 1.5, it will auto covert Integer to int and int to Integer.
whereas there is no autoboxing in jdk 1.4

--
Shivani


/** Code speaks louder than words */
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
would it not use the toString() method and and print the string for both object since it is in a print statement?


If it did that you would get "109" and "110" printed. The + would have to be taken as the concatenation operator.

In the case of ten + nine neither of the operands is a string, so the + is taken as the arithmetical add operator. Add needs primitives to operate upon. In 1.4 the wrapper classes canot be converted to primitives so you get a compilation error. As previously mentioned, in 5.0 an unboxing conversion takes place for the wrapper operands and the add operation succeeds.

Moral: if you are studying for SCJP 1.4, then use the 1.4 mocks and use the 1.4.2_<latest> compiler. If you are studying for SCJP 5.0, then be awake for these sometimes suprising changes.

Please don't shoot the mock author!


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

Joined: Aug 31, 2005
Posts: 481
For jdk 1.4
The wrapper object cannot perform "+" operator.
so it always result in false.
Right??
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Karthik Rajashekaran:
For jdk 1.4
The wrapper object cannot perform "+" operator.
so it always result in false.
Right??


For Java 1.5: It does not result in false - you get a compilation error.
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
I i'm using 1.5. Thanks all for clearing this point up for me.
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
ok, I've encountered this problem even on jdk1.4
the thing is simple
the toString() method is not called unless one of the arguments is a String

Not from the exam point of view, but it is best to use an "" whenever you print out primitives by themselves

print(3+4) prints 7
print(3+""+4) prints 34

Wrapped primitives are NOT eligible operands for mathematical operations, and you're sure to get an error, unless you do what I've mentioned.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper Question