File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper class 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 class" Watch "Wrapper class" New topic
Author

Wrapper class

Jason
Ranch Hand

Joined: Dec 06, 2000
Posts: 35
Here is an Mock exam question about Wrapper class, and I could not agree with the given answer:
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) Error: Can't convert java lang Integer
4) 10 followed by 1
Answer to Question 25
Error: Cant convert java lang Integer
The wrapper classes cannot be used like primitives.
Wrapper classes have similar names to primitives but all start with upper case letters.
Thus in this case we have int as a primitive and Integer as a wrapper. The objectives do not specifically mention the wrapper classes but don't be surprised if they come up.
Any help to clarify this?
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Hi Jason
Your question itself is the answer to your query. Wrapper classes cannot be used as primitives in addition or any kind of mathamatical operations b'coz they are creating Objects ! So we will get error on both println statements saying that "Incompatible type for +. Can't convert java.lang.Integer to int." and one similer message for java.lang.Long too.
Hope this helps
Thanks
ARS Kumar.

[This message has been edited by ARS Kumar (edited June 17, 2000).]


ARS Kumar, Sun Certified Programmer for Java 2 Platform
http://www.automatedsqa.com/
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Jason.
Like ARS said, ur question has given the answer.The point is that there can be no arithmetic operations on obj. references.Another point is that in any arithmetic operation,the operands are converted to at least primitive type int.
hence here what is happening is that the compiler is trying to convert java.lang.Integer to int.Remember there can be no operations between a obj. ref and primitives.So u r getting the error specified.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper class
 
Similar Threads
wrapper classes
mock question Wrapper issue
Qn on Wrapper classes.
Marcus Green Exam 1 Question 25
Wrapper Question