This week's book giveaway is in the Java in General forum. We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line! See this thread for details.
I'm just reading up on the + operator and the book I'm currently reading states that:
"For a + expression with two operands of primitive numeric types, the result is at least int, because of normal promotion".
Does that mean that two bytes added together will result in an int type, even if the sum of the two bytes is within in the byte range?
Thanks very much! Juliet
posted 11 years ago
Yes. But you can cast the result to a byte. Bear in mind that doing so could lose data. If you add two bytes valued at 0xFF then cast to a byte, the result will be 0xFE. If you left it at an integer, the result would be 0x1FE.
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)