Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers and Autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers and Autoboxing" Watch "Wrappers and Autoboxing" New topic

Wrappers and Autoboxing

Pinki Roy

Joined: May 16, 2008
Posts: 22

Why does line 4 give me compile time error where as line 3 does not give me any error.

Swastik Dey

Joined: Jan 08, 2009
Posts: 1584

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.

Pinki Roy

Joined: May 16, 2008
Posts: 22
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.
I agree. Here's the link:
subject: Wrappers and Autoboxing
It's not a secret anymore!