Introduction to Java byte
Every variable in java must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. There are eight different data types available in the java programming language. You can read more about the data types from the article on java data types. In this article, we will specifically focus on java byte data type.
We will see how to declare variables with java byte type and will solve various examples. We will also cover how to declare and initialize a java byte array and will perform different operations including typecasting and initializing an array using for loop. All in all, this tutorial, will contain all the necessary information and examples about java bytes.
Getting started with Java byte
A data type is an attribute of a variable that tells the compiler or interpreter how the programmer intends to use the variables. It defines the operations that can be done on the data and what type of values can be stored. The eight different data types in java can be categories into two main groups; Primitive data type and non-primitive data type.
A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods. While the non-primitive data type is defined by the programming language but is created by the programmer. They are also called “reference variables” or “object references” since they reference a memory location that stores the data. Now the byte is simply a small section/ area which can store information. In the upcoming sections, we will focus on java byte and will cover the kind of data that they can store and will solve various examples related to them.
Java byte data type
A group of binary digits or bits operated on as a unit is called byte. A java byte is considered as a unit of memory size. A byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason, it is the smallest addressable unit of memory in many computer architectures. A byte is a data measurement unit that contains eight bits, or a series of eight zeros and ones. A single byte can be used to represent 28 or 256 different values.
As we already discussed that the java byte is one of the primitive data types. This means that the Java byte is the same size as a byte in computer memory: it's 8 bits and can hold values ranging from -128
to 127
.
Declaring and initializing Java byte
Now we already have some theoretical knowledge about java byte. Let us now go into more details and start the implementation part. Here we will see how we can declare a java byte and initialize it to some values. See the syntax of java byte declaration below:
byte variableName;
We use the keyword byte to declare java byte typed variable and then the name of variable ending with a semi-colon. Once we declared a byte variable, then we can initialize it to any value which must be in between -128 to 127. See the initialization of the byte variable below:
variableName = value;
Another simple way is to declare and initialize the byte variable at the same time. For example, see the syntax below:
byte variableName = value;
Here on the left side, we declared the variable type byte and then at the same time initialize it to any valid value on the right side.
Example-1 Using Java byte type
Now we know how to declare and initialize byte data type in java. Let us take a practical example and see how we can use byte data type in our java programming language. See the java program below:
// java main class
public class Main{
// java main method
public static void main(String[] args) {
// java btye data type
byte num1 = 5;
byte num2 = 10;
// printing
System.out.println("The sum of variable is :"+ (num1+num2));
}
}
Output:
The sum of variable is :15
Notice that we added the two-byte variables using plus operators.
Example-2 Java byte type error
We already discussed that the byte can take values from -128 to 127 but let us see what will happens if we try to store value beyond the range. See the example below where we stored the value to 140 to a one-byte type variable.
// java main class
public class Main{
// java main method
public static void main(String[] args) {
// java btye data type
byte num1 = 5;
byte num2 = 140;
// printing
System.out.println("The sum of variable is :"+ (num1+num2));
}
}
When we run the program we will get the following error.
The error says that the byte value cannot contain a value of 140 and it cannot convert the byte to an integer ( which can contain values more than 127).
Java byte array type
An array in Java is a set of variables referenced by using a single variable name combined with an index number. Each item of an array is an element. All the elements in an array must be of the same type. You can read more about java arrays from the article on java arrays. As we already discussed that all elements of the array must be of the same type. We declare the type before initializing the array. In the following sections, we will see how we can declare and initialize a java array of type bytes.
Declaration and initialization of array byte in Java
We already had discussed that an array can store any type of element depending on the type we defined while declaring the array. Now we let us see how we can declare a java array of type byte. See the simple syntax below:
byte ArrayName[];
When we declare array variables in java, actually we only declare the variables (reference) to the arrays. The declaration does not actually create an array. We have to create an array. See the syntax below.
ArrayName = new byte[ArraySize];
Inside the square brackets, we have to provide the size of the array. It can be any integer value. We can also declare and defined byte array type in one line as well instead of declaring and initializing separately. See the syntax below:
byte[] arrayName = new byte[ArraySize];
Notice that we declared and initialized the byte array in one line.
Example of Java byte array
Now we already know to declare and initialize byte array in java. Let us take an example and see how we can practically implement a byte typed array in java. See the example below:
// importing Arrays
import java.util.Arrays;
// java main class
public class Main{
// java main method
public static void main(String[] args) {
// declaring byte type array!!
byte[] MyArray = new byte[]{1, 2, 3, 4, 5,6};
// printing the java byte array
System.out.println(Arrays.toString(MyArray));
}
}
Output:
[1, 2, 3, 4, 5, 6]
Notice that we were able to successfully printed all the byte array in java.
Converting String to Java byte array
A String is stored as an array of Unicode characters in Java. You can learn more about string from the article on java strings. To convert it to a byte array, we translate the sequence of characters into a sequence of bytes. For this translation, we use an instance of charset. This class specifies a mapping between a sequence of chars and a sequence of bytes. Now let us take an example and see how we can convert a string into a java byte array. See the example below:
// importing Arrays
import java.util.Arrays;
// java main class
public class Main{
// java main method
public static void main(String[] args) {
// creating java string
String MyString = "Welcome to golinxCloud!";
// converting a java string to byte array
byte[] MyArray = MyString.getBytes();
// printing
System.out.println(Arrays.toString(MyArray));
}
}
Output:
[87, 101, 108, 99, 111, 109, 101, 32, 116, 111, 32, 103, 111, 108, 105, 110, 120, 67, 108, 111, 117, 100, 33]
Notice that we get all the integer values as an output. It is because the getbytes()
method converts the string values into their corresponding ASCII value of alphabets are integer values. For example, the integer value in ASCII for 'W' is 87 as we get it.
Summary
We already discussed that there are eight primitive data types that are supported by the Java programming language and byte is one of them. The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). In this tutorial, we learned about the java byte in more detail. We covered how we can declare and initialize a byte type variable in java through various examples. At the same time, we had discussed what would happen if we provide a value larger than the range of byte.
Moreover, we also learned how we can create a byte typed array in java. Towards the end, we also covered how we can convert a string into a java byte array using an example. In a nutshell, this tutorial contains all the necessary information that you need to know in order to get started using java bytes.
Further Reading
Java bytes
Java data types
Java strings