1

Why am I not getting the output since byte value is from -128 to 127 and the result is 30 ?

class test15 {          
   public static void main(String ...s) {
      byte a = 10;
      byte b = 20;
      byte c = a+b;
      System.out.println(c);
   }    
}

7 Answers 7

5

CompileTime vs RunTime

a,b are bytes and bytes doesn't have addition operator. At run time, since a,b are not final and integer addition takes place which result is an int. And you are trying to assign it to a byte. Which results a compilation error.

byte c = (byte)a+b;

And as a side note. try to change your code as

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

That compiles fine because of narrowing conversion and it is possible, if all the operands are constant values in an expression.

2
  • Since Java 8, it is enough when they are effectively final, and they are in the OP's code
    – msrd0
    Sep 12, 2014 at 13:33
  • @msrd0 Yes, I agree with you. Sep 12, 2014 at 14:46
2

You must cast to byte. Since a and b are of type byte, Java will convert them to an int, and then the result is an int.

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

Compile time error, if I change this

byte c = a + b;

to

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

I get the output

30
1

Change

byte c = a + b;

to

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

It works for me.

2
  • i know the concept of typecast but what i am not able to understand is since byte value is from -128 to 127 value of C which is 30 also falls under it then why error ? Sep 12, 2014 at 13:29
  • because byte hasn't got additional operator, like sᴜʀᴇsʜ ᴀᴛᴛᴀ said. His solution is better than mine, use it instead. Sep 12, 2014 at 13:34
0

There is no instruction set to perform operation on a byte type. Rather the instruction set for int type is used for the operation on boolean, byte, char, and short types.http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.11.1 Also A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer

The result will be an Integer. For this you must do

byte a = 10;
byte b = 20;
byte c = (byte) (a + b); //u will have to typecast int to byte 

System.out.println(c);
6
  • i know the concept of typecast but what i am not able to understand is since byte value is from -128 to 127 value of C which is 30 also falls under it then why error ? Sep 12, 2014 at 13:28
  • Because that's how the Java Virtual Machine is designed. There is no instruction set to perform operation on a byte type. Rather the instruction set for int type is used for the operation on boolean, byte, char, and short types.
    – kirti
    Sep 12, 2014 at 13:30
  • Are you sure that short has no addition operator? I remember that short+short worked on Java7, but don't have Java7 on my PC here
    – msrd0
    Sep 12, 2014 at 13:38
  • yes u can check here JVM Spec - Section 2.11.1:docs.oracle.com/javase/specs/jvms/se7/html/…
    – kirti
    Sep 12, 2014 at 13:44
  • Why was this downvoted? This answer is spot on (though I recommend adding the JVM Spec link to the main text of the answer rather than leave it languishing in the comments section).
    – Bobulous
    Sep 14, 2014 at 15:04
0

Java converts the result of arithmetic operations to int type by default. Try this:

public static void main(String... s)
{

    byte a = 10;
    byte b = 20;
    byte c = (byte) (a + b);

System.out.println(c);
}

That (byte) changes the int value returned by (a+b) into a byte value. Normally, this cannot be done, as the range of int is much greater than the range of byte, so a loss of precision would occur. Using casting forces the conversion, regardless of precision.

1
  • 1
    so as per your saying "Java converts the result of arithmetic operations to int type by default" is for all variables like long, float, short ?? or just in case of byte ? Sep 12, 2014 at 13:26
0

Here's whats happening

byte a = 10;

variable a of byte type created 10 assigned to it.

byte b = 20;

variable b of byte type created 10 assigned to it.

byte c = a+b;

a+b is automatically promoted to integer, since now result of summation is integer you cannot assign integer to byte.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.