Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers and Autoboxing

 
Pinki Roy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik , thanks a lot for the clarification .
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic