Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch And Primitive Wrappers  RSS feed

 
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?
 
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!