File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Switch And Primitive Wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch And Primitive Wrappers" Watch "Switch And Primitive Wrappers" New topic
Author

Switch And Primitive Wrappers

Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

Hi

I'm wondering why I can't use an Integer as a case constant:



Autoboxing seems to work in the switch() statement, but not for the case?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sam Samson wrote:Hi

I'm wondering why I can't use an Integer as a case constant:



Autoboxing seems to work in the switch() statement, but not for the case?


Because auto-unbox turns case finalInteger into case finalInteger.intValue(), and calling intValue() makes it non a constant.
Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

Perfect explanation, thank you!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Switch And Primitive Wrappers
 
Similar Threads
using switch statement
Method Resolution
about final
Can't a switch use final parameters?
A switch-case statement question