Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch

 
umang bhartia
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why answer is False? can any one explain:
in a switch statement the argument to the case label (case: argument) can be any variable which can fit within an int?
thank u
 
Amit Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Amit
 
Anna s Iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type of the Expression must be char, byte, short, or int OR a compile-time error occurs.
So it cann't be any variable(double, float) unless with explicit casting.
Hope this helps,
Anna S Iyer.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
umang bhartia
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Simon Roberts
Author
Ranch Hand
Posts: 168
7
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

byte x;
:
:
case 256:

</pre>
Won't work either.
See JLS sections 14.9 and 15.27 http://java.sun.com/docs/books/jls/html/14.doc.html#35518 http://java.sun.com/docs/books/jls/html/15.doc.html#5313
Cheers,
Simon

Originally posted by umang bhartia:
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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic