GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why can't the wrapper classes be switch statement expressions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why can Watch "why can New topic
Author

why can't the wrapper classes be switch statement expressions?

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
For example, if I have the following:


Why can't i1 be a case expression?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Rachel Glenn wrote:For example, if I have the following:


Why can't i1 be a case expression?


Case statements requires compile time constants. Currently, with Java, Integer objects are not compile time constants.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Henry Wong wrote:
Rachel Glenn wrote:For example, if I have the following:


Why can't i1 be a case expression?


Case statements requires compile time constants. Currently, with Java, Integer objects are not compile time constants.

Henry


Is there is areason for that, or is that just the way it is? I find it confusing especially since wrapper objects are immutable.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Rachel Glenn wrote:
Is there is areason for that, or is that just the way it is? I find it confusing especially since wrapper objects are immutable.


Short answer ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28


As for the reason why, and/or whether this will change. you will need to contact the Java designers directly.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


Also... forgot to mention... although string literals has been compile time constants for a very long time, their usage in a case statement is relatively new. So, in the future, perhaps Java will support Integer objects in case statements, which of course, will require that they be supported as compile time constants first.

Henry

 
 
subject: why can't the wrapper classes be switch statement expressions?