Home› Java Programming Language› / Programs / Java Program to Demonstrate the byte Data Type
🚀 Programming Example

Java Program to Demonstrate the byte Data Type

3,267 Views
Java Java 8 or later
Beginner
Demonstration

Learn how to declare, initialise and print byte variables in Java. This program prints the range of byte using Byte.MAX_VALUE and Byte.MIN_VALUE. It also shows that adding two bytes gives an int, and what happens when a value is too large to fit in a byte.

Before You Begin

What you will learn

  • Declare and initialise variables of the byte data type
  • Know that byte uses 1 byte (8 bits) and stores values from -128 to 127
  • Print the range using Byte.MAX_VALUE and Byte.MIN_VALUE
  • Understand that arithmetic on byte values gives an int
  • Use a cast (byte) and understand overflow (wrap-around)
  • Know when byte is a good choice

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • What a variable is and how to declare one
  • Basic idea of the int data type
No previous program
No next program

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named ByteDataType.
  3. Define the main() method.
  4. Declare byte variable a and assign it 100.
  5. Declare byte variable b and assign it -50.
  6. Display the values of a and b.
  7. Display Byte.MAX_VALUE and Byte.MIN_VALUE.
  8. Add a and b, store the result in an int variable sum, and display it.
  9. Cast 130 to byte, store it in overflow, and display it.
  10. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac ByteDataType.java
java ByteDataType

💻 Program Code

public class ByteDataType {

    public static void main(String[] args) {

        // Declare and initialise byte variables
        // A byte can store whole numbers from -128 to 127
        byte a = 100;
        byte b = -50;

        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Range of the byte data type
        System.out.println("Max byte value = " + Byte.MAX_VALUE);
        System.out.println("Min byte value = " + Byte.MIN_VALUE);

        // byte + byte gives an int, so the result is stored in an int
        int sum = a + b;
        System.out.println("sum = " + sum);

        // Overflow: 130 is too big for a byte, so it wraps around
        byte overflow = (byte) 130;
        System.out.println("(byte) 130 = " + overflow);
    }
}

Sample Input

No input required

🖥 Program Output

                                a = 100
b = -50
Max byte value = 127
Min byte value = -128
sum = 50
(byte) 130 = -126
                            

📘 Explanation

Overview

byte is the smallest integer data type in Java. It stores whole numbers in 1 byte (8 bits) of memory, and its range is -128 to 127. It's useful when you need to save memory, for example in large arrays or when working with files and network data.

Unlike float, a byte needs no suffix. Java accepts byte a = 100; because 100 is a constant that fits within the byte range.

Step-by-Step Explanation

1

Declare and Initialise byte Variables

Values must be between -128 and 127.

byte a = 100;
byte b = -50;

byte is the data type, and a and b are the variable names. Both values fit within the byte range, so they're stored without any problem.

2

Print the Values

Text and values joined with +.

System.out.println("a = " + a);
System.out.println("b = " + b);

The + operator joins the label and the value, so the output is a = 100 and b = -50.

3

Range of byte

The largest and smallest values.

System.out.println("Max byte value = " + Byte.MAX_VALUE);
System.out.println("Min byte value = " + Byte.MIN_VALUE);

Byte is the wrapper class for byte. Byte.MAX_VALUE is 127 and Byte.MIN_VALUE is -128. With 8 bits, a byte can store $$2^8 = 256$$ different values, from $$-2^7$$ to $$2^7 - 1$$.

4

byte + byte Gives an int

Type promotion in arithmetic.

int sum = a + b;
System.out.println("sum = " + sum);

Before doing arithmetic, Java converts byte values to int. So a + b is an int, and the result 50 is stored in an int variable. Writing byte sum = a + b; gives a compile error.

5

Overflow with a Cast

Values outside the range wrap around.

byte overflow = (byte) 130;
System.out.println("(byte) 130 = " + overflow);

130 is larger than 127, so byte overflow = 130; won't compile. The (byte) cast forces it, and the value wraps around: $$130 - 256 = -126$$

byte at a Glance

Property Value
Size 1 byte (8 bits)
Range -128 to 127
Default value (fields) 0
Literal suffix None
Wrapper class Byte

Java Integer Types Compared

Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes About ±9.22 × 1018

Output

a = 100
b = -50
Max byte value = 127
Min byte value = -128
sum = 50
(byte) 130 = -126

Common Mistakes

Assigning a value outside the range byte a = 200; gives the error incompatible types: possible lossy conversion from int to byte.
Storing byte arithmetic in a byte byte sum = a + b; doesn't compile because a + b is an int. Use an int variable or cast the result with (byte).
Casting without checking the range A cast hides the error but doesn't fix it. (byte) 130 silently becomes -126.
Good practice Use int for everyday whole numbers. Use byte only when memory matters, such as large arrays or raw file and network data.

Key Takeaway

byte stores whole numbers from -128 to 127 in 1 byte. Arithmetic on bytes gives an int, and casting a value outside the range makes it wrap around.

Complexity

Time: O(1)

Space: O(1)

No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.