Why is there no need to explicitly cast in case of integers?

Viewed 122
byte a=10;
byte b=20;

b=a+b;

In this case, I need to explicitly convert a+b to byte like this :

b=(byte)(a+b);

It's the same with short :

short x=23;
short y=24;

Otherwise it gives an error.

But in case of integers, it's not required to convert explicitly :

int p=7788;
int q=7668;

p=p+q;

This will work just fine.

Why is that?

We don't need to explicitly cast even in the case of long as well.

3 Answers

If you look at the JLS 4.2.2 Integer Operations, it states that the result of a numerical operation between two integral operands is an int or a long. Since there's no implicit cast from an int to byte or a short, you need an explicit cast.

If you refer to JLS Sec 15.18.2, which deals with addition of numeric types, it says:

Binary numeric promotion is performed on the operands (§5.6.2).

...

The type of an additive expression on numeric operands is the promoted type of its operands.

JLS Sec 5.6.2 describes binary numeric promotion:

  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

    • If either operand is of type double, the other is converted to double.

    • Otherwise, if either operand is of type float, the other is converted to float.

    • Otherwise, if either operand is of type long, the other is converted to long.

    • Otherwise, both operands are converted to type int.

So, in the case of int and long (where both operands are of that type), binary numeric promotion is a no-op: the operands remain int and long respectively, and the result of the addition is int and long respectively, meaning the result can be assigned to variables of that type.

In the case of byte and short, binary numeric promotion causes both of those to be widened to int to perform the addition, and the result of the addition is int; you have to explicitly cast back again to the narrower type, because not all int values fit into a byte or short.


There are 2 exceptions to this requirement to do an explicit narrowing cast.

Firstly, compound assignments: this would have worked:

b += a;

because, as stated in JLS Sec 15.26.2:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

In other words: the compiler inserts the cast for you:

b = (byte) ((b) + (a));

Secondly, if the operands have constant values, and the result of the addition is known to fit into the range of the narrower type, and you are doing the assignment at the variable declaration.

For example:

final byte a=10;  // final is necessary for a and b to be constant expressions.
final byte b=20;

byte c = a + b;

This requires no cast.

With addition in java, java will promote the smaller data type to the larger one. And when the data type is smaller than int it will promote both the operant to int. Take a look at: Promotion in Java?

Related