File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rachel Glenn
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, if I have the following:


Why can't i1 be a case expression?
 
Henry Wong
author
Marshal
Pie
Posts: 20830
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rachel Glenn
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20830
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20830
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic