Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch case

 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
taken from: http://www.jdiscuss.com
int x = Integer.parseInt(args[0]);
switch(x)
{
case x < 5 : System.out.println("BIG"); break;
case x > 5 : System.out.println("SMALL");
default : System.out.println("CORRECT"); break;
}
this is giving compilation error. but as i know parseInt will convert String into primitive, so it should atlaest compile
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it won't compile because of case, not because of switch.Case should be compile time constant and should be int here case results in boolean.
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Switch , case statement should be consatant /static final.
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The value given in the case should be constants of type that can implicitly casted to int type.ie, we can use byte, short or int type constants.

In the example, you had used boolean, which can't be casted to int.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression used should be a constant one.
//compiles fine
eg: final int x=10;
switch(12):
case x-1:
case x:

If you have just int x=10; it will give compiler error.

But when i tried to compile the below code:
//gives compiler error that const expression is reg
final int x=Integer.parseInt("12");//here i am keeping the value of x as constant.
eg: final int x=Integer.parseInt("12");
switch(12):
case x-1:
case x:

Kindly suggest!
 
Khp Virajith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Just to add some facts, always remember that switch structure can be used ONLY to check the equality between the switch expression and the case values.
Also remember that only byte, short, int and char primitive types are the ONLY possible expression/case values for a switch.

And one special point, type of the case values must be compatible with the switch expression type.



code given above results in a compile time error. If you look at carefully you can catch the point.




data type of the value 1000 is int and it's not compliant with the switch expression's data type which is byte.

Good Luck!

Regards,
VIRAJ
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidhya



This problem may arise because x gets value only at runtime. In the definition of switch case construct, it is specified that the case argument should be a compile time constant.

The same error message will get if you use,
int y=20;
final int x=y+1;

But, the error will be resolved if you change that to,
final int y=20;
final int x=y+1;

Because in the latter case, as y is a constant, x also get the value at compile time itself.

I am not sure whether this is cent percentage correct. If i am wrong, please correct it.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. I got the point.
Thanks Anoob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic