Re: short + short = int????
short + short might be an int, right?
This is why the Java compiler considers the sum of 2 shorts as an int.
There is a solution to this problem - and by the way it's not that much of a problem - , a simple command line argument when compiling But I can't seem to remember it.
Re: short + short = int????
Quote:
Originally Posted by ComputerJy
short + short might be an int, right?
This is why the Java compiler considers the sum of 2 shorts as an int.
int + int might be a long but the Java compiler doesn't consider the sum of 2 ints as a long though.
Seems strange that it doesn't automatically try to cast it back down to a short if it can.
I'd be interested in that compiler option if you can manage to dig it out.
Re: short + short = int????
Java Lang Specs says:
‘Binary numeric promotion is performed on the operands (§5.6.2). The type of a
multiplicative expression is the promoted type of its operands. If this promoted type is
int or long, then integer arithmetic is performed; ...If an integer multiplication overflows,
then the result is the low-order bits of the mathematical product as represented in some
sufficiently large two’s-complement format. As a result, if overflow occurs, then the sign of
the result may not be the same as the sign of the mathematical product of the two operand
values.’
In reality Java performs Integer summation on int and long data types only
If you want more about this topic you can read this paper