Originally posted by rajashree ghatak:
hi all,
i am unable to understand how the following code is legal?
byte b = 10;
switch(b)
{
case 'a' :
break;
}
whereas
char ch='a';
byte b=ch; //Explicit cast needed to convert char to byte.
[b]or
byte b=97;
char ch=b; //Explicit cast needed to convert byte to char.
According to Mughal book,"The case label values must be assignable to the type of integral expression used in switch statement."
could anyone explain why char the above switch statement would compile without error where as the next 2 assignment statements will give compile time errors?
thanx in advance.
rajashree.[/B]
SCJP2. Please Indent your code using UBB Code
SCJP2, SCWCD
Originally posted by Adrian Muscalu:
It's strange while the previous piece of code works, this one doesn't:
final byte b;
{
b=97;
char ch = b;
}
Any ideas regarding the cause? Please excuse for the "off-topic" message.
t's strange while the previous piece of code works, this one doesn't:
final byte b;
{
b=97;
char ch = b;
}
Simple names that refer to final variables whose initializers are constant expressions
Simple names that refer to final variables whose initializers are constant expressions
SCJP2. Please Indent your code using UBB Code
However, the reverse of the above will always require casting.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
SCJP2. Please Indent your code using UBB Code
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|