This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, Try to imagine if the varible is of type double or long or float say..... double x =10; case: x we can not put like thiss even the value of x can fit into an int. I hope this will solve your doubt.... Bye
This is true for the switch part of the statement but not the case part. Don't get the two confused. For this part: switch(x), x must be able to fit within an int. So byte, char, short, or int will work. But for this part: case:x, x must be a constant value. The compiler must be able to determine what x is at compile time. So it has to either be something like case:x where x is a final value, or it could look like this case:1+2. That is why it is false. Bill
Joined: Sep 29, 2000
Thanks every 1 I think Bill is right, in (case:argument) the argument must be either an int literal or an in int constant ie final. thanks once again
The argument to switch must be <em>assignement compatible</em> with int. That is, the <em>type</em> must be int, or a smaller numeric type. The argument to case must be a compile time constant. This, however, includes constant expressions, such as: <pre> private static final int x = 9; : : case 5 + x:
</pre> Further, the constants used in the case statements must take values small enough to be represented by the type of the switch. So, this: <pre>