Can we add two byte variables and assign the result to a
byte variable ?
b=b1+b2 where b,b1,b2 are byte types

Answers were Sorted based on User's Feedback



Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / rajesh

Yes,the following is the right way to do so
byte b1=21;byte b2=11;
byte b;
b=(byte)(b1+b2);

Is This Answer Correct ?    39 Yes 5 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / m. abhishek

yes, but u require type casting because the the binary
operation in java should atleast returns Integer values


example :
byte a=12;
byte b=23;
// byte c=a+b; //Not OK,bcoz a+b is an int value
int c=(byte)a+b; // OK

Is This Answer Correct ?    34 Yes 7 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / ashish ranjan

No We cannot add two byte variables directly. The reason behind is the operator '+' is used to perform integer operation. so the variables are automatically promoted to the integer when we perform any arithmetic operation.

so we need to typecast the variables

b= (byte)(b1 + b2);

Is This Answer Correct ?    15 Yes 2 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / guest

yes.But you have to cast that variable before adding.

eg:
byte b=(byte)b1+(byte)b2

Is This Answer Correct ?    7 Yes 35 No

Post New Answer

More Core Java Interview Questions

What is an example of character?

0 Answers  


Can a class inherit the constructors of its superclass?

9 Answers   iFlex, Wipro,


Why method overriding is used?

0 Answers  


there are N number of matchboxes numbered 1...N.each matchbox contain various number of stick.Two player can alternatevely pick some amount of stick from the higest stick containing box . The player is condidered win if there is no stick after his move.Find the final move so that the move player win. Note:In case the number of stick is equal ,pick the stick from the higest numbered box.

0 Answers   Manhattan,


Write a program for recursive Traverse?

1 Answers  


What are nested classes in java?

0 Answers  


Is 'sizeof' a keyword?

7 Answers  


Is object a data type?

0 Answers  


What are inbuilt functions in java?

0 Answers  


Is java supports multiple inheritance? explain?

12 Answers   BUET, Inforica,


What do you mean by object?

0 Answers  


What is the difference between an if statement and a switch statement?

0 Answers  


Categories