Ali, Satya,
byte b = (flag==true) ? 0 : 1; This is an example of
assignment conversion; From
JLS here special type of narrowing conversion is also there when all of the foll conditions are true.
1. The expression is a constant expression of type int.
2. The type of the variable is byte, short, or char.
3. The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
byte b = (flag==true) ? 0 : 1; when we use a
var(not constant) flag in an expression it is NOT COMPILE TIME CONSTANT. The variable's value may change at run time. We can't be sure. This means the right hand side of the above statement is NOT COMPILE TIME constant expression and the evaluation of the right hand side of the statement is common promoted type of the 2 operands between the : . Here both are 'int' So the compiler says Can't convert 'int' to 'byte' at compile time. IF the operands at both sides of the : WOULD HAVE BEEN int : float then the compiler will say can't convert float to byte. This rule is applicable when both operands are reference types also. One operand MUST be convertible to the other.
So, when you apply
flag==true the resulting type of the above statement becomes int which CAN'T be assigned to a var of type byte.
At the same time when the WHOLE of right hand side expression involves ALL Compile time constants like
true == true and the resulting int literal value is between the range of the left hand var type which here is 'byte' ,then this expression is ELIGIBLE for the above said
special type of assignment conversion So the foll.
byte b = (true==true) ? 0 : 1; same as
byte b = true ? 0 : 1; same as
byte b= 0; You can learn more about the terinary operator
here in JLS regds
maha anna
[This message has been edited by maha anna (edited April 19, 2000).]