Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch statement

 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
huh, thats interesting to know because I thought it should require typecasting but may be not. THanks Shankar!
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, is that how it works? good to know.
Thanks Bill!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic