Converting Integer Data Type to Byte Data Type Using Typecasting in Java Last Updated : 01 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In primitive casting, we can convert the byte data type information into the integer data type information by using implicit typecasting. When we try to convert the integer data type information into the byte data type information that time explicit type casting takes place. Explicit Type of Casting Converting higher data type information into lower data type information. Syntax: Datatype1 variable1 = information; Datatype2 Variable2 = (Datatype2) Variable1 Here Datatype1 = Higher Data type Datatype2 = Lower Data type In this article, we are going to convert integer to byte. Example 1: Given int a = 1000, when we convert it to byte data type information it will show the output -24, it is due to the byte range being from -128 to 127. Java public class IntToByte { public static void main(String args[]) { int a = 1000; byte b = (byte)a; System.out.println(b); } } Output: -24 Explanation: Java public class IntegerByte { public static void main(String args[]) { int c = 1000; // as byte range from -128 to 127 int d = 128; // Conditions if our value // is positive or negative int x = c / d; int f = x + 1; int g = x - 1; int e = (c > 0) ? f : g; if (x % 2 == 0) { System.out.println(c - d * x); } else { System.out.println(c - d * (e)); } } } Output: -24 Here we divided the byte range into two parts. -128 to 00 to 127 And we have given conditions in two parts by using the if-else statement. Let's see one more example. Example 2: Java public class IntegerByte { public static void main(String args[]) { int c = -2000; // as byte range from -128 to 127 int d = 128; // Conditions if our value // is positive or negative int x = c / d; int f = x + 1; int g = x - 1; int e = (c > 0) ? f : g; if (x % 2 == 0) { System.out.println(c - d * x); } else { System.out.println(c - d * (e)); } } } Output: 48 Comment More infoAdvertise with us Next Article Convert Long Values into Byte Using Explicit Casting in Java B badesuraj9 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Convert String to Byte Array in Java Using getBytes(encoding) Method In Java, any sequence of characters within double quotes is treated as String literal. String class represents the character string literal. The string class is present in java.lang package. All string literals in java are immutable i.e their value cannot be changed once created. In Java, the string 3 min read Convert Long Values into Byte Using Explicit Casting in Java In Java, a byte can contain only values from -128 to 127, if we try to cast a long value above or below the limits of the byte then there will be a precision loss. 1. byte: The byte data type is an 8-bit signed twoâs complement integer. Syntax: byte varName; // Default value 0 Values: 1 byte (8 bits 2 min read Convert String to Byte Array in Java Using getBytes(Charset) Method To convert a string to a byte array, we use the getBytes(Charset) method In Java that transforms the string into a sequence of bytes based on a specified character encoding. This method returns the byte array and relies on the Charset class to handle character-to-byte mappings.Example:Java// Java pr 3 min read Java Program to Convert String to Byte Array Using getBytes() Method In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an 2 min read 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 ha 2 min read How to Convert InputStream to Byte Array in Java? In Java, input stream refers to an ordered flow of data in the form of bytes. This flow of data can be coming from various resources such as files, network programs, input devices, etc. In order to read such data, we have a Java InputStream Class in the Java IO API. There are several methods to conv 5 min read Like