12

I am trying to add two values in a byte array. This is my code:

byte[] ars = {3,6,9,2,4};
ars[0] = (byte)ars[0] + (byte)ars[4];
System.out.println( ars[0] );

I get this error on compilation:

Main.java:9: possible loss of precision
found   : int
required: byte
    ars[0] = (byte)ars[0] + (byte)ars[4];
                          ^
1 error

Any help is, as always, much appreciated.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Figitaki
  • 139
  • 1
  • 1
  • 11

7 Answers7

14

close, but a little off.

ars[0] = (byte)(ars[0] + ars[4]);

keep in mind ars[0] and ars[4] are already bytes, so there is no need to cast them to bytes.

Instead, cast the result of the summation to a byte.

edthethird
  • 6,263
  • 2
  • 24
  • 34
  • 5
    It happens because 100 (a byte) + 100 (another byte) is 200, which is not a byte (in Java, where bytes are from -128 to 127). The explicit cast says that it is okay to throw away the "overflow". – Thilo Mar 22 '12 at 03:26
13

In Java, the sum of two bytes is an int. This is because, for instance, two numbers under 127 can add to a number over 127, and by default Java uses ints for almost all numbers.

To satisfy the compiler, replace the line in question with this:

ars[0] = (byte)(ars[0] + ars[4]);
Taymon
  • 24,950
  • 9
  • 62
  • 84
  • 2
    I do not think your answer's reasoning is valid. The sum of two `int`s can also be over `Integer.MAX_VALUE`. However, `int a = Integer.MAX_VALUE + 1` does not give a compile-time error, while `byte a = 1 + 1` does give a compile-time error. This is all due to the JVM's design, specified in the JVM spec section 2.11.1, details of which are too technical for many people – Gokay Nov 12 '18 at 23:52
  • See https://stackoverflow.com/a/18483732/9304999 . It would be more accurate and detailed answer. – rosshjb Nov 14 '20 at 09:34
5

I cam across this question a while back, and collected all the findings here : http://downwithjava.wordpress.com/2012/11/01/explanation-to-teaser-2/

We all know bytes get converted to ints during an arithmetic operation. But why does this happen? Because JVM has no arithmetic instructions defined for bytes. byte type variables have to be added by first 'numerically promoting' them to 'int' type, and then adding. Why are there no arithmetic instructions for the byte type in JVM? The JVM spec clearly says:

The Java virtual machine provides the most direct support for data of type int. This is partly in anticipation of efficient implementations of the Java virtual machine's operand stacks and local variable arrays. It is also motivated by the frequency of int data in typical programs. Other integral types have less direct support. There are no byte, char, or short versions of the store, load, or add instructions, for instance.

4
public static void main(String args[]){
    byte[] ars = {3,6,9,2,4};
    ars[0] = (byte)(ars[0] + ars[4]);
    System.out.println( ars[0] );
}

this happens for the reason that java automatically converts expressions which use byte and short variables to int... this is to avoid potential risk of overflow.... as a result even if result may be in the range of byte java promotes type of expression to int

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
flyinrhyno
  • 159
  • 1
  • 7
2

Please replace that line with the following

ars[0] = (byte)(ars[0]+ars[4]);
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
2
ars[0] = (byte)(ars[0] + ars[4]);
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
2
byte[] ars = {3,6,9,2,4};
        ars[0] = (byte) (ars[0] + ars[4]);
        System.out.println( ars[0] );

This will work, try it

gmhk
  • 15,598
  • 27
  • 89
  • 112