Java Program to Calculate Sum of Two Byte Values Using Type Casting



When we convert a data type to another data type, we call it type casting. There are two types of type casting in Java: explicit and implicit.

Explicit Type Casting 

When we typecast a one datatype to another using the cast operator "()", it is known as explicit type casting, and it is done manually.

We typically use the cast operator to convert a higher datatype to a lower datatype. Therefore, there is a risk of losing data because a lower datatype has a range smaller than a higher datatype.

Implicit Type Casting

But, to convert a smaller datatype to a larger one, there is no need to use the cast operator. The compiler automatically (implicitly) converts a smaller datatype to a larger datatype at the time of assignment.

This is known as widening or implicit type casting. In this case, there is no risk of data loss at the time of conversion.

Byte Datatype in Java

Since byte is the lowest datatype available in Java, we need to convert it into a higher data type (widening). Since the compiler takes care of it implicitly, there is no need for us to use the cast operator.

We can assing a byte value to short, char, int, long, float, and double.  

Sum of Byte Values by Type Casting to Integer

Both integer and byte are primitive datatypes, but the main difference between them is the range of storage size. A byte can store 1 byte of data only but an integer can store 4 bytes of data. Hence, the byte datatype can be typecast to an integer easily.

We use a byte in place of an integer when we require a memory size less than 1 byte. It will save some of the computer's memory.

Example

In the below code, we are demonstrating the sum of two byte values using implicit type casting. Here, given byte values will be typecast to the integer datatype implicitly.

public class Main{
   public static void main(String[] args) {
      byte by1 = 122, by2 = 54;
      int n1 = by1;
      int n2 = by2;
      int intSum = n1 + n2;
      System.out.println("Sum after type casted to integer: " + intSum);
   }
}

The result after running the above code is as follows:

Sum after type casted to integer: 176

Sum of Byte Values by Type Casting to Double

Double stores fractional data, but byte stores whole numbers only, and double can store 8 bytes of data that is larger than the byte datatype. So, byte can also be typecasted to double easily.

Example

Here, again, we have taken two byte values, but this time they are type-cast to double datatype implicitly.

public class Main{
   public static void main(String[] args) {
      byte by1 = 122, by2 = 54;
      double d1 = by1;
      double d2 = by2;
      double doubleSum = d1 + d2;
      System.out.println("Sum after type casted to double: " + doubleSum);
   }
}

In the output, the decimal point indicates that the byte value is successfully converted to a double datatype:

Sum after type casted to double: 176.0

Need of Type Casting while Adding Two Byte Values

The range of the byte datatype is between -128 to 127 only. If the sum is in between the range, then it is fine.

But if we add two byte values, such as 100 and 29, whose sum is 129, then the overflow problem may occur because 129 is greater than the range of a byte.

So, we need to convert a byte into a higher datatype to minimize the risk of losing our data.

Example

Following is an example -

public class Main{
   public static void main(String[] args){
      byte by1 =122, by2=54;
      byte byteSum;
      byteSum= (byte) by1+by2;
      System.out.printlln(byteSum);
   }
}

After running the above code, we will get an error. Here, the compiler is warning us that there is a lossy conversion because the value of sum is 129 and it is exceeding the range of the byte datatype. We are losing some data.

Main.java:5: error: incompatible types: possible lossy conversion from int to byte
byteSum= (byte) by1+by2;
                           ^
Main.java:6: error: cannot find symbol
System.out.printlln(byteSum);
                  ^
  symbol:   method printlln(byte)
  location: variable out of type PrintStream
2 errors
Updated on: 2025-06-19T15:38:06+05:30

501 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements