On 5/19/06, Levi Pearson <[EMAIL PROTECTED]> wrote:
I wrote a little test program, and your typecasting issue is not an
issue, and the parentheses are not necessary.  I haven't looked at
the spec to see what exactly causes the bytes to get promoted to
ints, but they do.  It's probably the fact that a literal number is
interpreted as an int, not a byte, so byteval << intval causes the
byte to be promoted to an int.  I ran into this by attempting to pass
literal numbers to a function that took two bytes, and having the
compiler complain that I was passing ints.

                --Levi

In Java, all math operations (with non-static data) promote lesser
numbers to int values.

byte mathIsFun(byte a, byte b) {
 return a + b; // ERROR!
}

Why is this an error?  Because java automagically promotes the bytes
to ints and the result of the statement is an int -- the return value
is too small to hold the int that is produced by the addition.  Java
does this to avoid common datatype under-runs.  If you really did want
a byte, then just cast down:

return (byte) a + b;  // No error.

-Bryan

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to