2

When we do addition,subtractions,divide(etc..)in byte short data types the result is coming from int value...why is that...?

e.g:- in above code does not compile because the result in c is coming from int why is that happen...??? when we try to print c using system out it came error because result is in int

public class A {
    public static void main(String[] args) {

        byte a = 12;
        byte b = 10;
        byte c = a + b;
        System.out.println(c);
    }
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
Paradox
  • 31
  • 3

5 Answers5

2

According to java doc the when we perform operations on two bytes there is probability that the output would be a number which can't be handled by byte, so it was decided by creators of java to auto cast it to Integer. So it's like this.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
2

This is well answered here, starting from an identical scenario :) http://www.coderanch.com/t/499127/java/java/Adding-bytes

To quote http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.2

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary: If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then: 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.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/10433317) – Jeen Broekstra Dec 03 '15 at 19:53
  • 1
    Added the essential nugget :) – Aidan Moriarty Dec 03 '15 at 20:35
0

The spec says:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2

The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.

The binary - operator performs subtraction, producing the difference of two numeric operands.

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

Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

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

https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

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

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.

If you combine the two you see that any operand in an addition will be promoted to either int or long and any primitive smaller than int will be promoted to int. The result of the operation is the type of both operands - which it int.

The reasoning behind that is most probably that int is the most common datatype which reduces probability of overflow while not requiring as much space as a long. The space argument probably isn't as generally valid as it used to be anymore but that part of the spec was defined like in 1995 and an inherent part of the language (which you can't change without breaking compatibility).

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Any Binary arithmetic operations we performed on char & byte datatype(and short) promote it to int. For more details please refer JLS 5.6.2.

Zia
  • 1,001
  • 1
  • 13
  • 25
0

This works:

final byte a = 12;
final byte b = 10;
byte c =  a + b;

Or this:

byte a = 12;
byte b = 10;
byte c = (byte)( a + b);
Julien Vavasseur
  • 3,854
  • 1
  • 20
  • 29