• 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

somebody please explain me this

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
In round-up i saw this question:
In switch stmt,argument to the case label(case:argument) can be any variable which can fit within an int.
I said true,
i got it wrong.
The answer says:
false
the case argument must be either an int literal or an int -compatible variable which is a constant(i.e. static final)
Thanx in advance,
Best Regards,
leena.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi leena,
I think you were just confused about the question. The question refers to the argument following the case construct not the switch.
In the switch construct --> switch (expression) --> expression can be anything that is compatible to an int.
On the other hand, for the case construct, it has to be an int constant.
For example:
int a = 5;
int b = 10;
switch (a*2) // this is possible, value is evaluated
{
case 10: // should be a constant int
case b: // this is not possible since b is a var
}
I hope this helps.


 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that:
final int x = 20; // OK to use 'x' in 'case' because of 'final'
Regards, Guy
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Cheery and
Very good point Guy Allard
please also note tht expressions involving final variables (along with literals only), which results in value assignable to the type of swicth-expression , can also be used as case labels

output is :
Good Luck for ur exam!

even float , double final-variables can be used in the expression , but the final result has to be cast into int's ( byte , char , short , int )

still , output is :
Good Luck for ur exam!

------------------
Gagan (/^_^\)
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Cherry,Guy and Gagan 4 such nice explanation .
Now the point is clear to me!!
Best Regards,
leena
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Gagan!
------------------
azaman
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic