Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java byte Keyword
Usage and Examples



byte Keyword in Java

In Java, the byte keyword is a primitive data type used to define 8-bit signed integers. It's the smallest numeric type in Java and ranges from -128 to 127. Due to its small size, it is mostly used in situations where memory optimization is critical — like working with raw binary data, image processing, or arrays with large volumes of numeric data.

Why use byte?

If you're dealing with values that fall within a small range, like age in years or color values (0–255, with adjustments), using byte helps reduce memory usage compared to using int or short. Java uses 1 byte (8 bits) of memory for byte, which is significantly less than the 4 bytes used by int.

Syntax

byte variableName = value;

Example: Declaring and Printing a Byte

public class ByteExample {
    public static void main(String[] args) {
        byte age = 25;
        System.out.println("Age is: " + age);
    }
}
Age is: 25

Byte Range and Overflow

The valid range of a byte is from -128 to 127. If you go beyond this, it wraps around — this is called overflow or underflow.

public class ByteOverflow {
    public static void main(String[] args) {
        byte max = 127;
        byte min = -128;

        max++;  // Overflow
        min--;  // Underflow

        System.out.println("After overflow: " + max);
        System.out.println("After underflow: " + min);
    }
}
After overflow: -128
After underflow: 127

Type Casting with byte

Since byte has a smaller range, assigning values from larger data types (like int or double) requires explicit casting. Be cautious here — casting can cause data loss.

public class ByteCasting {
    public static void main(String[] args) {
        int num = 150;
        byte small = (byte) num;
        System.out.println("Casted byte value: " + small);
    }
}
Casted byte value: -106

This happens because 150 is outside the range of byte. Java wraps the value using 2's complement logic.

Arithmetic with byte

Even though you're working with byte values, arithmetic operations like addition or subtraction promote them to int by default. So the result must be cast back to byte if needed.

public class ByteArithmetic {
    public static void main(String[] args) {
        byte a = 10;
        byte b = 20;

        // byte c = a + b; // This will cause a compilation error
        byte c = (byte)(a + b);
        System.out.println("Result: " + c);
    }
}
Result: 30

Working with Byte Arrays

Byte arrays are useful for reading and writing binary data (like files or network streams). Here’s how to define and use one:

public class ByteArrayExample {
    public static void main(String[] args) {
        byte[] data = {10, 20, 30, 40};

        for (byte b : data) {
            System.out.print(b + " ");
        }
    }
}
10 20 30 40 

When Not to Use byte

While byte is memory-efficient, it’s not always the best choice:

Final Thoughts

The byte keyword in Java is a compact and efficient data type for small numeric values. While not as commonly used as int, it plays a crucial role in systems where performance and memory optimization are essential. Understanding its behavior, range, and quirks—especially overflow and casting—is key to using it effectively.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M