Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch case Compiler error

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code from Whizlabs..I am getting compile error at line 17 "case expressions must be constant expressions "..Why?
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"case expressions must be constant expressions "..Why?


That's a requirement of the language. Case values must be constants that can be determined at compile time.

Interestingly, Java is not the only language that requires this.

Henry
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x4 is not a case expression here ? I see the value of 8 is assigned to x4.
"case expressions must be constant expressions "..Why?
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:x4 is not a case expression here ? I see the value of 8 is assigned to x4.



Integer objects are not defined as a type to be treated as a compile time constant -- so... no.

Henry
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means, Auto-unboxing happens at Runtime and not at compile time..Is that statement right ?
Henry Wong wrote:
Saibabaa Pragada wrote:x4 is not a case expression here ? I see the value of 8 is assigned to x4.



Integer objects are not defined as a type to be treated as a compile time constant -- so... no.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saibabaa Pragada wrote:That means, Auto-unboxing happens at Runtime and not at compile time..Is that statement right ?


Autoboxing is just syntactic sugar. This ...



is the same as this...




As for unboxing, that requires a method call too, which of course, isn't allowed for compile time constants either.

Henry
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation.Thank you Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic