Dremendo Tag Line

Byte Variable in Java Programming

Java Basic Concepts

In this lesson, we will learn, what is Byte Variable and how it works in Java programming along with some examples.

What is Byte Variable

The smallest integer data type available in Java is byte. It can store values within the range -128 to 127. The byte data type can be useful while working with a stream of data over a network or a file.

video-poster

Syntax of Declaring Byte Variable in Java

byte variable_name;

Here byte is used for declaring Byte data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).

Now let's see some examples for more understanding.

Example 1

Declare a byte variable x.

byte x;

Example 2

Declare a byte variable x to assign a byte value 10.

byte x = 10;

Example 3

Declare 3 byte variables x, y and z in a single line.

byte x, y, z;

Example 4

Declare 3 byte variables x, y and z to assign the byte value 42, 67 and 85 respectively in a single line.

byte x=42, y=67, z=85;

Example 5

Declare a byte variable x and assign the byte value 18 in the second line.

byte x;
x = 18;

Example 6

Declare a byte variable x and assign the byte value 76 and change its value to 54 in the next line.

byte x = 76;
x = 54; // now the new value of x is 54

Test Your Knowledge


You Scored :

Percentage :