File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch case" Watch "switch case" New topic
Author

switch case

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
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

Joined: Mar 15, 2008
Posts: 210

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.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
In Switch , case statement should be consatant /static final.


Thanks,<br />Subhasish
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
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.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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!


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Khp Virajith
Ranch Hand

Joined: May 01, 2006
Posts: 85
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


SCJP, SCWCD, SCMAD, SCSNI
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
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

Joined: Aug 28, 2008
Posts: 148
Oh. I got the point.
Thanks Anoob.
 
 
subject: switch case