- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 as type casting. There are two types of type casting in java explicit and implicit. When we type cast a higher datatype to a lower data type then, it is called explicit type casting and it is done manually. In it, there is a risk of losing data because lower datatype has range smaller than a higher datatype, this is the main reason of doing it manually.
Since, byte is the lowest datatype available in java, we need to type cast it into a higher data type. It can be type casted to short, char, int, long, float and double. So, we are going to perform sum of two byte values using type casting by implicit conversion. In this case, the conversion of byte datatype to any other higher datatype is done automatically by compiler and also during the process of conversion there is no risk of losing data.
Sum of Two Byte Values by Type Casting to Integer Datatype
Both integer and byte are primitive datatypes but the main difference between them is the range of storage size. Byte can store 1 byte of data only but an integer can store 4 bytes of data. Hence, byte datatype can be type casted to integer datatype easily.
We use byte in place of integer when we require memory size less than 1 byte. It will save some of the computer’s memory.
Example
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); } }
Output
Sum after type casted to integer: 176
In the above code, we have demonstrated the sum of two byte values using implicit type casting. ‘by1’ and ‘by2’ are the two byte values that are type casted to integer datatype implicitly. ‘intSum’ is the integer variable that holds the values of their sum.
Sum of Two Byte Values by Type Casting to Double Datatype
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
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); } }
Output
Sum after type casted to double: 176.0
Here, again we have taken ‘by1’ and ‘by2’ as two byte values but this time they are type casted to double datatype implicitly and ‘doubleSum’ is the double type variable that holds the values of their sum. In the output, decimal point is indicating that byte value is successfully converted to double datatype.
Why Type Casting needed while calculating sum of two Byte values?
The range of 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, overflow problem may occur because 129 is greater than the range of byte. So, we need to convert byte into some higher datatype to minimize the risk of losing our data.
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); } }
Output
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
After running the above code we are getting this error. Here, compiler is warning us that there is a lossy conversion because the value of sum is 129 and it is exceeding the range of byte datatype. We are losing some data.
Conclusion
In this article, we have calculated the sum of two byte values using implicit type casting into integer and double datatype and we also understood the need for type casting. During type casting, values of variables didn’t get changed only the datatype of that variable gets changed. Compatibility of datatypes is also considered during type casting.