Author Topic:   Conditional Operator
posted April 19, 2000 05:54 AM           
Hi all.
This is the first time I'm posting, so be nice to me please.

1. public class TestA
2. {
3. public static void main(String[] args)
4. {
6. boolean flag = true;
7. byte b = (flag==true) ? 0 : 1;
8. System.out.println(b);
9. }

Understandably , I get a compile error on line 6 :
Incompatible type for declaration. Explicit cast needed to convert int to byte.

If I change line 6 to:
byte b = (true==true) ? 0 : 1;

This compiles okay. Is it because of compiler optimisation on the modified line no 6 making it equal to:
byte b = 0;

Any ideas?

posted April 19, 2000 07:08 AM           
Sorry, any references I made to line 6 in my comments should read line 7.

maha anna
posted April 19, 2000 07:23 AM             
Hello Ali,
Welcome. Fell free to post your doubts here. One small suggestion. In this forum you can edit your messages by clicking the small icon at the top-right of your message (see.. there is a small icon with a paper-and-pencil) which is well used by Maha most of the time.
maha anna

ranch hand
posted April 19, 2000 07:31 AM             

1. Is compiler optimization in the objectives of SCJP2
Programmer certification ?
2. If it is, could someone explain this please ?
3. If not please do not post such topics in this forum. I
have no intention of being harsh or anything, but such
topics really scares me.


- satya

maha anna
posted April 19, 2000 07:41 AM             
Don't worry. Compiler optimization is NOT in the SCJP2 exam objectives. I left this thread because, we can consider this qstn as part of operators and assignamets. Take it easy.
maha anna

maha anna
posted April 19, 2000 01:28 PM             
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

maha anna

[This message has been edited by maha anna (edited April 19, 2000).]

Betty Reynolds
ranch hand
posted April 19, 2000 02:04 PM         
(Maha, I was composing my post at the same time you were revising yours, so I didn't see the above. I think you've confirmed my statement. Thanks.)

This is a good question. It kind of stumped me at first, and it brought out something about conditional expressions that I wasn't aware of.

What I think is happening here is that if you use a variable in the boolean part of the conditional expression, then the second and third operands are subject to (binary?) numeric promotion and an explicit cast is required.

If you use a boolean literal, then the second and third operands are treated just as they are (i.e., constants of type int whose value can fit into the byte variable and therefore no cast is required) and the conditional expression is treated as a simple assignment.

Can someone confirm?

[This message has been edited by Betty Reynolds (edited April 19, 2000).]