Can anyone help me, the whole statement here is being casted to 'byte'. So the casted value will be truncated to its byte value but in the answers they dont even give you the byte option to select and answer is 'double'. I compiled and checked it out and the value is a double. I cant figure it out, hoe can this return a double value when it is casted into a 'byte'? Does it not cast a complicated statement into a byte or something? can any1 help. Thanks in advance.
43. What will be the return type of this statement? (byte)10/10.2*7;
Sunil, I'm studying for the exam, and here's the operator precedence I have in my notes (got most from somewhere on web, then made a couple of additions): Highest precedence on top of list: 1. postfix ops:  . (params) i++ i-- 2. unary ops: ++i --i +i -i ~ ! 3. creation or cast: new (type)expr 4. multiplicative: * / % 5. additive: + - 6. shift ops: >> << >>> 7. relational: > < >= <= instanceof<br /> 8. equality: == !=<br /> 9. bitwise/logical AND: &<br /> 10. bitwise/logical XOR: ^<br /> 11. bitwise/logical OR: |<br /> 12. short circuit AND: &&<br /> 13. short circuit OR: | |<br /> 14. conditional: a?b:c<br /> 15. assignment: = += -= *= /= %= >>= <<= >>>= &= ^= |=
The way it should work, for your statement: (byte)10/10.2*7; a. integer literal 10 cast to a byte = 10 b. / and * are same precedence, so evaluated left to right. So, division evaluated first. On right hand side of / is byte, left hand side has double, so byte is automatically promoted to double. 10.0/10.2 = 0.9... c. That result is multiplied by 7. Again, "7" is an integral literal so it is promoted to 7.0 and the result is a double that is probably pretty close to 6.9. Make sense?