Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type of (long + long)

Tags:

java

it seems to be a silly question but I couldn't find the answer or at least I didn't know how to formulate it on Google.

When we add two bytes in Java, the result is an int.

But what about two long, or a long and an int ? Same questions for double and float. I'm a little bit lost between all those conversions.

like image 526
SegFault Avatar asked Jan 17 '26 21:01

SegFault


1 Answers

A long. This is subject to §5.6.2 Binary Numeric Promotion, item 2:

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

You get an int when adding two bytes because of the last rule.

like image 164
kennytm Avatar answered Jan 19 '26 20:01

kennytm