aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch statement 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 statement" Watch "Switch statement" New topic
Author

Switch statement

Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Again this question is from JQ+
The following methos will compile and run without any problems. True or False?
public void switchTest(byte x) {
switch(x) {
case 'b': //1
default:
case -2:
case 80:
}
}
The answer is True.
But a character 'b' at line 1 is not assignable to a byte, then shouldn't the compiler give error? Surprisingly it does compile on jdk1.2.
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi,
byte b = 'b' will compile fine.try printing its value and it will be 98 i.e. the ascii value of 'b'.
one important point to note is as long as u assign values in the range -128 to 127 to a byte variable , there wont be any problems.so ur code will compile fine.
shanks.
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
huh, thats interesting to know because I thought it should require typecasting but may be not. THanks Shankar!
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
hi,
byte b='b';
will not compile.
public void switchTest(byte x) {
switch(x) {
case 'b': //1
default:
case -2:
case 80:
}
}
then how this code will compile?
jeena jose
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Right, I just checked and byte b = 'b' does not compile, explicit cast is required.
But then how come my code compiles?
Posting the code again for those who haven't read the previous part of thread.

I am using jdk1.2.2
anybody???
[This message has been edited by Seema Das (edited May 11, 2001).]
S Parameswaran
Greenhorn

Joined: May 11, 2001
Posts: 2
Hi,
The byte will be implicitly converted to integer and then the switch will work on the integer. Thats the reason the code gets compiled without any problem.
See below :

public void switchTest(byte x) {
switch(x) { // Here x will be coverted
// to integer
case 'b': //1 // here 'b' will be
//converted to integer
default:
case -2:
case 80:
}
}
In all the other cases, the numbers themselves are valid integers.

[This message has been edited by S Parameswaran (edited May 11, 2001).]
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
Hi seema,
Remember byte,short char get's implicitly converted to integer when the compiler reaches the switch statement then the next line compiler sees the case which is char and therefore assignable to int type as a result compiler does not throw any error.
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi,
byte b = 'b' is compiling fine in jdk1.3 compiler.
code:
public class test{
public static void main(String[] args){
byte b = 'b';
System.out.println(b);
}
}
output: 98
i dunno why its not compiling for u all.
shanks.
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Originally posted by nitin sharma:
Hi seema,
Remember byte,short char get's implicitly converted to integer when the compiler reaches the switch statement then the next line compiler sees the case which is char and therefore assignable to int type as a result compiler does not throw any error.

Sorry, to disagree but thats not true. Byte does not get converted to int. byte remains byte. To test this, if we put case 200:, compiler gives error that 200 is too large for byte.
So that means the argument is still byte.
Another thing to mention is, JQ+ says that this code does not compile on jdk1.2 but compiles on 1.3. Then in this kind of scenario, should the answer be false(since we are giving test for jdk platform 2) or should it be true since it compiles on 1.3?
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
I suspect you folks have been testing slightly different versions.
void someMethod( byte x ){
switch(x) {
case 'b':
default:
case -2:
case 80:
}
Although 'b' is type char, it is converted to int by the compiler - this value is acceptable because it is in the -128 - 127 range for bytes. The -2 and 80 values are also in the correct range.
Since all case values can be reached by a byte, the compiler does not object.
Remember, values in case statements are evaluated at compile time.
Bill
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Oh, is that how it works? good to know.
Thanks Bill!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch statement