Guys,
My opinion about some of the issues are as follows:
1)
Thomas: Regarding your first post, you ask: "This code compiles and runs without a hitch eventhough values of type byte CANNOT be assigned to variables of type char without an explicit" - Please remember that the expression within a switch(
expr) must be a byte, short, char or int.
- Next, the values for all the case labels MUST BE COMPILE TIME CONSTANTS.
Upto this point, I hope, everybody agrees.
Next, please note that
- the case constant expr must be assignable to that switch expr.
At this point Thomas asks, why doesn't he get a compiler error?
Well the answer is:
When we have a compile time constant expr that could be REPRESENTED AS A byte, short, char or int being assigned to a variable of type byte, short or char(obviously
we don't need an int here !) then an implicit narrowing coversion is done here automatically.
Fine...., lets look at some examples:
byte b = 100; // compile time constant and can be narrowed down //to a byte
int i = 50; // compile time constant
byte b1 = 300; // compiler error ! coz 300 is out of range and //cannot be represented in a byte variable.
consider,
byte b = 100;
final int i = 10;
final int j = 20;
b = i+ j + 10; // Here you have an autamatic narrowing coversion as the expr i + j + 10 is compile time const.
The same thing applies to the switch-case expressions.
Assume that we have byte b;
When we say
switch(b)
{
case 100:
case
some value or expression case 300:
----
-----
}
then the compiler checks if it can assign each case label constants to the byte variable b.
i.e b = 100; // ok
b = 300; // OOOps out of range - compiler error!
Therfore, in Thomas's case code below,
switch (charExpression) {
default:
System.out.println("default");
break;
case b:
}
an implicit narrowing conversion is automatically done as:
i.e. charExpression = 0x41; //same as line final byte b = 0x41;
That's the reason u don't get compiler error. But had you put a negative value as srikish did, the compiler immediately screams coz a char variable can't have -ve values.
(2)
Explict Cast: For all other regular expression(typically non compile time constant expressions), you need an explicit cast if you are performing a narrowing conversion.
short s = 100;
char c = 50;
c = s; // compiler error
s = c; // compiler error
Also, you can see that values in the variables in s & c can be changed at run-time(eg i/p from user for these values).
So, the compiler will have no idea about these run time values and cannot possibly check its validity before assigning. SO, java insists that you put an explict case assuming that the programmer knows what he is doing.
(3) Mapraputa, you are wondering why float f = 2.3 gives a compiler error!
Well, we all know that all floating point literals are automatically assumed as to be double values unless suffixed by an 'F' or 'f'.
I really can't give an accurate answer, all I can tell you is that representing floating point values as a double
is basicaly done for efficient manuplation of the values. (I remember vaguely reading this somewhere)
Since, compiler assumes 2.3 to be a double and therefore assigning to a float means loss of precision. So, the compiler screams!
(4)michael huang, I don't get any error for the code you have given.
Plus 0x41 = 65 in decimal.
Hope this helps
-sampaths77