This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com