⬅ Previous Topic
Java break KeywordNext Topic ⮕
Java case Keywordbyte
Keyword⬅ Previous Topic
Java break KeywordNext Topic ⮕
Java case Keywordbyte
Keyword in JavaIn 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.
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
.
byte variableName = value;
public class ByteExample {
public static void main(String[] args) {
byte age = 25;
System.out.println("Age is: " + age);
}
}
Age is: 25
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
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.
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
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
byte
While byte
is memory-efficient, it’s not always the best choice:
int
is preferred due to native processor support.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.
⬅ Previous Topic
Java break KeywordNext Topic ⮕
Java case KeywordYou 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.