The addition of two-byte values in java is the same as normal integer addition. The byte data type is 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The Sum of two-byte values might cross the given limit of byte, this condition can be handled using storing the sum of two-byte numbers in an integer variable.
Input: firstByte = 10, secondByte = 23 Output: Sum = 33 Input: firstByte = 10, secondByte = 23 Output: Sum = 33
Type Casting: Typecasting is simply known as cast one data type, into another data type. For example, in this program, Byte values are cast into an integer.
- Declare and initialize the two-byte variable.
- Declare one int variable.
- Store the sum of the two-byte numbers into an int variable.
Below is the implementation of the above approach
sum = 236
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.