Convert Long Values into Byte Using Explicit Casting in Java Last Updated : 29 Oct, 2020 Comments Improve Suggest changes Like Article Like Report 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) : -128 to 127 2. long: The long data type is a 64-bit two’s complement integer. Syntax: long varName; // Default value 0 Values: 8 byte (64 bits): -9223372036854775808 to 9223372036854775807 Example 1: In limits Java // Java Program to Convert Long (under Byte limit) // Values into Byte using explicit casting import java.io.*; class GFG { public static void main(String[] args) { long firstLong = 45; long secondLong = -90; // explicit type conversion from long to byte byte firstByte = (byte)firstLong; byte secondByte = (byte)secondLong; // printing typecasted value System.out.println(firstByte); System.out.println(secondByte); } } Output45 -90 Example 2: Out of limits Java // Java Program to Convert Long (out of the // limits of Byte) Values into Byte using // explicit casting import java.io.*; class GFG { public static void main(String[] args) { long firstLong = 150; long secondLong = -130; // explicit type conversion from long to byte byte firstByte = (byte)firstLong; byte secondByte = (byte)secondLong; // printing typecasted value System.out.println(firstByte); System.out.println(secondByte); } } Output-106 126 Comment More infoAdvertise with us Next Article How to Convert a Byte value to String value in Java with Examples P parasmadan15 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen 4 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 How to Convert a String value to Byte value in Java with Examples Given a String "str" in Java, the task is to convert this string to byte type. Examples: Input: str = "1" Output: 1 Input: str = "3" Output: 3 Approach 1: (Naive Method) One method is to traverse the string and add the numbers one by one to the byte type. This method is not an efficient approach. Ap 2 min read How to Convert a String value to Byte value in Java with Examples Given a String "str" in Java, the task is to convert this string to byte type. Examples: Input: str = "1" Output: 1 Input: str = "3" Output: 3 Approach 1: (Naive Method) One method is to traverse the string and add the numbers one by one to the byte type. This method is not an efficient approach. Ap 2 min read How to Convert a Byte value to String value in Java with Examples Given a Byte value in Java, the task is to convert this byte value to string type. Examples: Input: 1 Output: "1" Input: 3 Output: "3" Approach 1: (Using + operator) One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will 2 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Like