4
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.

rajkumar.11
  • 313
  • 2
  • 9
  • Because the result of any arithmetic operation is at least an integer wide, and an integer is already an integer. – user207421 Jun 24 '19 at 06:53

3 Answers3

8

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • if we take value of a and b such as sum of a+b is greater than 127 its still giving error if we take a=70 and b=80 final byte a=70; final byte b=80; byte c = a + b; will give error same is the case when value of a+b is less than -128 – rajkumar.11 Jun 24 '19 at 12:20
  • @rajkumar.11 "and the result of the addition is known to fit into the range of the narrower type". – Andy Turner Jun 24 '19 at 12:31
2

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?

Willem
  • 992
  • 6
  • 13
  • It's not very precise to say "smaller data type": a `long` will be promoted to a `float`, and even though `float` is smaller in the sense that it's 32 bits vs `long`'s 64 bits. – Andy Turner Jun 24 '19 at 07:18