Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: does not apply to types shorter than int

...

Addition (as with all arithmetic operations) in Java is performed on signed numbers only as unsigned numbers are unsupported. One exception is the unsigned char type. Performing arithmetic operations that use operands of type char is strongly discouraged.

Noncompliant Code Example

...

If the result of the addition is greater than the maximum value or less than the minimum value that can be represented as an int, then the variable temp will contain an erroneous result. This does not apply to shorter types such as byte and short because the operands are promoted to an int before the operation is carried out. The compiler disallows storing the result of the operation in a variable of type shorter than an int.

Compliant Solution (Bounds Checking)

...