Arrays and Strings · Introduction to Java Programming

Byte Class

Watch lectures

Byte Class

1. What Is Byte? (beginner)

byte = tiny int (1 byte, -128 to 127). Byte = its wrapper (object form for lists, parsing, null). Use for files, images, network bytes (memory-saving!).

2. Code — Parse + Range Proof

byte b = 100; // primitive (fits!)
System.out.println(b + 28); // 128 (int math! byte+int → int!)
// byte c = 200; // ERROR! (200 > 127 — out of range!)
Byte obj = Byte.valueOf("100"); // parse "100" → Byte(100)
System.out.println(obj.byteValue() + " min=" + Byte.MIN_VALUE + " max=" + Byte.MAX_VALUE);
// min=-128 max=127
byte over = (byte) 258; // manual wrap: 258 % 256 = 2 (cast overflow!)
System.out.println(over); // 2

3. Key Constants + Methods

Member Use
Byte.MIN_VALUE / MAX_VALUE -128 / 127
Byte.parseByte("100") String → byte (throws if >127!)
obj.byteValue() wrapper → primitive
Byte.valueOf("100") cached object form

4. Traps

  • byte + byte = int (promotion!): byte c = a + b; ERROR — need (byte)(a+b).
  • parseByte("200") → NumberFormatException (out of range!).
  • Prefer int for math; byte for storage/arrays.

5. Exam Points

  • Range + parse + promotion trap — 3-5 marks, frequent.
Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

← All Introduction to Java Programming modules