| Author |
Wrappers and Autoboxing
|
Pinki Roy
Greenhorn
Joined: May 16, 2008
Posts: 20
|
|
Why does line 4 give me compile time error where as line 3 does not give me any error.
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
|
sum1 is an Integer type object, and that's why it's allowing method calls. But sum2 is not any reference of any class, it's primitive type data. As you can't call any method on primitive type, this is the reason java introduces the concepts of these wrapper classes, for e.g. Integer,Double.
|
Swastik
|
 |
Pinki Roy
Greenhorn
Joined: May 16, 2008
Posts: 20
|
|
|
Swastik , thanks a lot for the clarification .
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
As a friendly reminder, there will be a compilation error if we do this
This is in one of the practice exam.
But this will work:
The new Integer(1) will be unboxed and assigned to a.
|
 |
 |
|
|
subject: Wrappers and Autoboxing
|
|
|