Java Program to Calculate Sum of Two Byte Values Using Type Casting
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