✏️ Explanatory Question

[Primitive Values, Wrapper Classes, Types and Casting]

Since a byte can represent 256 different numbers, why is its maximum value 127?

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Because Java uses signed representation for byte, half of the values are used for negative numbers.

Step 1: Understanding Byte Size

A byte in Java uses:

8 bits = 1 byte

Total combinations:

2⁸ = 256 values

Step 2: Signed vs Unsigned Representation

Java uses signed (two’s complement) representation for integers.

This means:

  • One bit is reserved for sign
  • Remaining bits store value

Step 3: Distribution of Values

Type Range
Negative values -128 to -1
Positive values 0 to 127

So total values:

128 negative + 128 positive = 256 values

Step 4: Why Maximum is 127?

The highest positive value is:

01111111 (binary) = 127

The leftmost bit (MSB) is used for sign:

  • 0 → Positive
  • 1 → Negative

Since one bit is used for sign, maximum positive number becomes:

+127

Step 5: Minimum Value

The smallest value is:

10000000 (binary) = -128

Important Concept:

Range of byte = -2⁷ to 2⁷ - 1 = -128 to 127

Conclusion:

  • Byte has 256 total values ✅
  • Java uses signed representation ✅
  • Values are split into positive and negative ✅
  • Hence maximum value = 127