• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch case Compiler error

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code from Whizlabs..I am getting compile error at line 17 "case expressions must be constant expressions "..Why?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation.Thank you Henry
reply
    Bookmark Topic Watch Topic
  • New Topic