Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch And Primitive Wrappers

 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
IntelliJ IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect explanation, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic