What is the difference between byte and char in Java?

The byte and char are two numeric data types in Java and both can represent integral numbers in a range but there are very different from each other. The main difference between a byte and char data type is that byte is used to store raw binary data while other is used to store characters or text data. You can store character literal into a char variable e.g. char a = 'a'; A character literal is enclosed in single quotes. In terms of range, a byte variable can hold any value from -128 to 127 but a char variable can hold any value between 0 and 255. Another difference between byte and char in Java is that the size of the byte variable is 8 bit while the size of the char variable is 16 bit. One more difference between char and byte is that byte can represent negative values as well but char can only represent positive values as its range is from -128 to 127. In other words, a byte is a signed data type where the first byte represent the sign of number i.e. 0 for positive and 1 for a negative number, but char data type is unsigned. Let's see some more differences between byte and char in Java.

byte vs char data type in Java

One of the key thing to know about byte and character types is how do you convert byte to char? this is the area where most programmer struggle because you need a character encoding to map raw bytes to the character. Same bytes will map to a different character in different character encoding, thus you must use same character encoding while converting byte to char and vice-versa.

In Java, classes which do this conversion e.g. FileReader or InputStreamReader by default uses platform's default character encoding, which may or may not be correct. You can further read Core Java for the Impatient to learn more about converting bytes to characters in Java.

Anyway, let's see some more points to understand the difference between byte and char data type better.

1) The first and foremost difference between byte and char are that byte is a signed data type while char is an unsigned data type. In signed data type first bit always represent a sign of the number.

2) From above fact, you can deduce another difference between byte and char that former can represent negative values but char values are always positive.

3) Another difference between char and byte is that char is larger data type than a byte. The range of byte is between -128 to 127 but the range of char is from 0 to 65535 because byte is a signed 8-bit data type and char is an unsigned 16-bit data type hence, its maximum value is 2 ^ 16 - 1 which is 65535.

4) You can initialize a char variable using character literal e.g. char ch = 'c', in this case, ASCII value of character 'c' will be stored into char variable "ch".

5) The wrapper class corresponding to byte primitive is java.lang.Byte and the wrapper class corresponding to char primitive is java.lang.Character class.

That's all about the difference between byte and char data type in Java. If you know any other difference which you think is important to learn, then feel free to add. You can also read Core Java Volume 1- Fundamentals to learn more about different data types and when to use them in Java programs e.g. float, double, long, int, boolean and short.