byte keyword is an 8-bit and smallest integer primitive type in Java. It ranges from -128 to 127 value.
It can be used especially in large-sized arrays to save memory. It is also one of the useful types for dealing with a stream of data from a file. Its default value is 0.
Syntax:
byte variableName = 2;
Since, byte type supports lowest range of value (-128 to 127). If one tries to assign a larger value to it, then the Java compiler would throw InputMismatchException with the message “Value out of range. Value:”3466″ Radix:10” at runtime. On other hand, at compile time, assigning a larger value would give an “incompatible type” exception.
Printing Java Byte Type
Byte is a signed type, meaning, it can hold positive and negative numbers. Below is the program to demonstrate the same:
public class ByteExamples { public static void main(String[] args) { byte bytNum = 67; byte negNum = -67; System.out.println("Byte positive value :: "+ bytNum); System.out.println("Byte negative value :: "+ negNum); } }
Output:
Byte positive value :: 67 Byte negative value :: -67
Byte can store lower range of values than int type. It is of 8-bit, on other hand, int type is of 32-bit. Therefore, a java compiler automatically converts byte type to int type, this is termed as implicit typecasting. There are other ways to, such as built-in methods.
We can directly assign a byte value to integer type. It gets automatically converted to byte.
Java program for converting byte to int using implicit typecasting
public class ByteExamples { public static void main(String[] args) { byte bytNum = 67; int num = bytNum; System.out.println("By Implicit typecasting :: "+ num); } }
Output:
By Implicit typecasting :: 67
Byte wrapper class also provides a method named intValue() that returns int value of that byte object. Below is the program.
Java program to convert byte to int using intValue() method
public class ByteExamples { public static void main(String[] args) { byte bytNum = 67; int num = new Byte(bytNum).intValue(); System.out.println("By intValue :: "+ num); } }
Output:
By intValue :: 67
toUnsignedInt method returns int type by unsigned conversion. It means, the argument for that method can allow byte value as an input ranging from 0 to 255.
Java program to convert byte to int using toUnsignedInt() method
public class ByteExamples { public static void main(String[] args) { byte bytNum = 67; int num = Byte.toUnsignedInt(bytNum); System.out.println("By toUnsignedInt :: "+ num); } }
Output:
By toUnsignedInt :: 67
As we know, a byte can have values ranging from -128 to 127 which is quite lower than int type. If one tries to store a int value to byte type, then a program outputs a lossy conversion. In other words, an int value within the range between -128 to 127 gets successfully converted to byte type. If the value is not having in the range, then Java cuts-off leftmost 24-bits for byte conversion, in such case, one gets different output then the provided integer type value.
Below program to demonstrate lossy conversion from int to byte
public class ByteExamples { public static void main(String[] args) { int num = 140; byte bytNum = (byte) num; System.out.println("data loss while converting int :: "+num+" to byte :: "+ bytNum); } }
Output:
data loss while converting int :: 140 to byte :: -116
Lets convert the above input value, 140, to binary form
0000_0000_0000_0000_0000_0000_1000_11000
cut off 24 left most bit
1000_11000
The most significant bit (MSB) is 1 [left most value], meaning the value is negative
remove MSB, we would get = 0001100,
Now calculate 2’s complement, in which first perform invert operation and then add 1 to the value.
now, invert it = 1110011
add 1 to it = +1
Output : 1110100
which is equivalent to = 116, since, leftmost bit was 1, so, the final result would be -116
Java program to convert int to byte using explicit typecasting
public class ByteExamples { public static void main(String[] args) { int num = 23; byte bytNum = (byte) num; System.out.println("using typecasting "+ bytNum); } }
Output:
using typecasting 23
Java program to convert int to byte using explicit byteValue method
public class ByteExamples { public static void main(String[] args) { int num = 23; byte bytNum = new Integer(num).byteValue(); System.out.println("using byteValue "+ bytNum); } }
Output:
using byteValue 23
This post was last modified on July 4, 2022