File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes widening and boxing... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "widening and boxing..." Watch "widening and boxing..." New topic
Author

widening and boxing...

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
--------------------------------------------------------------------------------
code

class WidenAndBox {
static void go(Long x) { System.out.println("Long"); }

public static void main(String [] args) {
byte b = 5;
go(b); // must widen then box - illegal
}
}

// why cant i widen first and then box... byte can be widened to long and then long can be boxed to Long... then why is it illegal...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13395

// why cant i widen first and then box... byte can be widened to long and then long can be boxed to Long... then why is it illegal...


Simply, because it doesn't. The specification is very specific on what it does.

If you want it to widen and then box, you have to explicitedly widen it first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: widening and boxing...
 
developer file tools