Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrappers and Autoboxing  RSS feed

 
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: 1791
15
Android Eclipse IDE 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!