• 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

Doubt about Switch statement (Compile Time Constant)

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt with the switch statement.

A switch's expression must evaluate to a char, byte, short, int, or, as of Java
6, an enum.
& the case constant must be a compile time constant!
Since the case argument has to be resolved at compile time, that means you can use only a constant or final variable that is assigned a
literal value.


Now why does the below statement fails ??



final int a = 1;
final int b;
b = 2;
int x = 0;
switch (x) {
case a: // ok
case b:
// compiler error


If I put SOUT statement below line (b=2), Value of b is printed as '2'.
then why doesn't switch statement accept it??

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose say if 'b' is accepted and below code compiles without error; when you will think case b: will get executed?

 
Rahul Bansode
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code is not getting compiled.....!!!

It's giving an error on case : b as "Case expressions must be Constant Expression"...!!!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the point I was making - if it gets compiled, you will be able to change the value of b in the case a. And it will result in case b never executed.

Hence case statements can be compile time constants.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:That was the point I was making - if it gets compiled, you will be able to change the value of b in the case a. And it will result in case b never executed.

Hence case statements can be compile time constants.


Your example doesn't address Rahul's question. In Rahul's original example, b was a final variable, so you wouldn't be able to change its value in case a. The only difference between a and b in Rahul's example was how they were initialised.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joanne... I was mistaken on the question... I understood that now...
 
Rahul Bansode
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joanne....!!

If I initialize the final variable at the time declaration (final int b = 2), The Switch case expression works fine.....!!!

but shows compilation error for Switch case expression




Hope you understand my question???


 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could have been designed to allow that - as you say, it could interpret b as a compile-time constant. But in that case the compiler's got to do more work. As things are now all it has to do is check the line the variable is declared on. To allow your code it would have to tell the difference between that and:
and similar constructions.

Added to that - the only reason you ever need to initialise a final variable on a different line to where it's declared is if the value is determined at run-time. Since there's no good reason for you to write the code like that, there's no good reason for the compiler to support it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Bansode wrote:Hope you understand my question???


Yes. I understood what you were asking. I just wasn't sure for certain what the answer was. What Matthew says is likely to be the reason, but if you want to know for certain why it works that way, you will probably need to read the Java Language Specification. Personally I would just accept that that is the way it is and not worry too much.
 
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

Rahul Bansode wrote:If I initialize the final variable at the time declaration (final int b = 2), The Switch case expression works fine.....!!!

but shows compilation error for Switch case expression



Hope you understand my question???




The answer is simple. The "b" variable is *not* a compile time constant. The JLS has many requirements for a variable to be a constant variable, and having it initialized during declaration is one of those requirements.

Henry
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this doesn't address the question(sorry), but i just wanted to mention that as of java7 we can use Strings also.
 
Rahul Bansode
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Joanne .
I got it .

Thanks alot guys for your discussion!!!







 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Bansode wrote: . . . A switch's expression must evaluate to a char, byte, short, int, or, as of Java 6, an enum. . . .

It has accepted enums since Java5. Now, as Randall Twede has already told you, a String can be used.
In the older version, you could consider it an int, or anything which can be explicitly cast to an int.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic