aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch" Watch "switch" New topic
Author

switch

umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
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

Joined: Oct 18, 2000
Posts: 52

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

Joined: Aug 21, 2000
Posts: 24
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Sep 29, 2000
Posts: 60
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
Greenhorn

Joined: Oct 24, 2000
Posts: 16
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


author of:<br /><ul type="square"><li><a href="http://www.amazon.com/exec/obidos/ASIN/0782128254/electricporkchop" target="_blank" rel="nofollow">The Complete Java 2 Certification Study Guide</a><li><a href="http://www.amazon.com/exec/obidos/ASIN/0782121799/electricporkchop" target="_blank" rel="nofollow">Java 2 Developer's Handbook</a></ul>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch
 
Similar Threads
//question of round-up//
java round-up game #223
Question 95 Rules Roundup
java ranch #223 and #325
Legal arguments in switch case statements