Bryan Sant wrote:
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.

That's interestingly surprising. I suppose I'd know that if Java were my primary language.

Shane

/*
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