Author | Topic: Conditional Operator |
Ali unregistered |
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 Understandably , I get a compile error on line 6 : If I change line 6 to: This compiles okay. Is it because of compiler optimisation on the modified line no 6 making it equal to: Any ideas?
|
Ali unregistered |
posted April 19, 2000 07:08 AM
Sorry, any references I made to line 6 in my comments should read line 7. Thanks.
|
maha anna bartender |
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. regds maha anna
|
satya5 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. Regds. - satya
|
maha anna bartender |
posted April 19, 2000 07:41 AM
satya, 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. regds maha anna
|
maha anna bartender |
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; 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. You can learn more about the terinary operator here in JLS regds [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).]
|
| | |