byte

In Java, a byte is a primitive data type that represents whole numbers. It is 8 bits in size (1 byte of memory) and can store values in the range -128 to 127.

Declaring a byte

You can declare a byte variable like this:

byte count = 20;
byte temperature = -9;

Tip: Use byte when you know the number will always be small and you want to save memory.

Why Use Byte?

byte is one of four integer types in Java:

TypeSizeRange
byte8 bits (1 byte)-128 to 127
short16 bits (2 bytes)-32,768 to 32,767
int32 bits (4 bytes)-2,147,483,648 to 2,147,483,647
long64 bits (8 bytes)-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

byte is often used for:

  • Low-level programming (e.g., working with streams, files, or network data)
  • Handling small numbers
  • Saving memory in large arrays

Primitive Nature of byte

As a primitive type, byte:

  • Stores the value directly in memory, not a reference
  • Does not have methods of its own
  • Replaces the old value when updated
byte a = 15;
a = 20;  // 15 is replaced by 20

How a Java byte Stores Values

A Java byte uses 8 bits to store a number:

Think of a bit as a tiny switch:

  • 0 = OFF
  • 1 = ON

A byte has 8 switches side by side.

[ b7 ][ b6 ][ b5 ][ b4 ][ b3 ][ b2 ][ b1 ][ b0 ]
  • The leftmost bit (b7) is special → it is the sign bit
  • The remaining 7 bits store the number 7 bits

The Sign Bit (Most Important Rule)

  • If the first bit is 0 → the number is positive
  • If the first bit is 1 → the number is negative

Examples:

01111111 // this equals 127
10000000 // this equals -128

How to Convert A Positive Number To Binary

Example – number 18:

Step 1: Divide 18 by 2

18 ÷ 2 = 9 // remainder 0  ← least significant bit (rightmost)

Step 2: Divide 9 by 2

9 ÷ 2 = 4 // remainder 1 

Step 3: Divide 4 by 2

4 ÷ 2 = 2 // remainder 0 

Step 4: Divide 2 by 2

2 ÷ 2 = 1 // remainder 0 

Step 5: Divide 1 by 2

1 ÷ 2 = 0 // remainder 1

Step 6: Reverse all the remainders to form – “10010”

You can verify in Java:

byte n = 18;
System.out.println(Integer.toBinaryString(n));
10010 // output

Tip: You can add three zeros at the front to form 8 bits and make it fit a byte (8 bits) – “00010010”

Byte Wrapper Class

Java provides a wrapper class Byte to add functionality to the primitive byte.

Useful Constants

ConstantExampleDescription
MAX_VALUEByte.MAX_VALUEMaximum byte value: 127
MIN_VALUEByte.MIN_VALUEMinimum byte value: -128
SIZEByte.SIZESize in bits: 8
BYTESByte.BYTESSize in bytes: 1

Converting Values

MethodExampleDescription
parseByte(String)Byte.parseByte("123")Converts a numeric string to a byte. Throws NumberFormatException if invalid.
toString()Byte myByte = 42; myByte.toString()Converts a byte to a numeric string.

Comparing Bytes

MethodExampleDescription
compare(byte a, byte b)Byte.compare(1, 2)Returns 0 if equal, -1 if a < b, 1 if a > b.
compareTo(Byte other)Byte x = 10; x.compareTo((byte)12)Returns -1, 0, or 1 depending on comparison.
equals(Byte other)Byte x = 10; x.equals((byte)12)Returns true if values are equal, false otherwise.

Utility Methods

MethodExampleDescription
max(byte a, byte b)Byte.max(1, 2)Returns the greater number.
min(byte a, byte b)Byte.min(1, 2)Returns the smaller number.
sum(byte a, byte b)Byte.sum(1, 2)Returns the sum of two bytes.

Quick Summary

  • byte is a small integer type (8 bits, -128 to 127).
  • Ideal for memory-efficient storage of small numbers.
  • Primitive type → stores values directly in memory.
  • Wrapper class Byte provides methods and constants for extra functionality.

Next Steps


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories