Java Integer byteValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, boxed.byteValue() converts an Integer to a primitive byte, an 8-bit number from -128 to 127. The method belongs to Number, so it exists since Java 1.1.

A value outside that range does not throw an exception. Java keeps only the low 8 bits, so 200 becomes -56 and 256 becomes 0.

An instance method of java.lang.Integer (since Java 1.1): call it on an Integer object. Integer lives in java.lang, so no import is needed.

Syntax

Call it on an Integer. It takes no arguments.

Java
Integer a = 100;
byte x = a.byteValue();        // 100
Integer b = 200;
byte y = b.byteValue();        // -56 (wrapped!)
int back = y & 0xFF;           // 200 again
CallWhat it does
boxed.byteValue()The low 8 bits as a byte.
(byte) iThe same narrowing for a primitive int.
b & 0xFFTurns a byte back into an int from 0 to 255.
Byte.MIN_VALUE / Byte.MAX_VALUEThe limits of byte: -128 and 127.

byteValue() never throws. Remember that a Java byte is always signed, so values from 128 to 255 turn into negative numbers.

Library declarations
Java
// java.lang.Integer
public byte byteValue()

// Inherited contract from java.lang.Number

Parameters

ParameterDescription
NonebyteValue() takes no parameters.

Return Value

Return ValueDescription
byteThe low 8 bits of the value, read as a signed number from -128 to 127.

Compare with Similar Methods

Related conversions:

MethodHow it differs
shortValue()Narrows to 16 bits instead of 8.
intValue()Keeps the full 32 bits.
toHexString(i)Shows the bits that byteValue() keeps, in hex.
reverseBytes(i)Reorders the four bytes of an int.

Java Examples

Example 1 — Narrow Numbers to byte

Convert a mix of small and large values and see which ones survive.

Java
public class IntegerByteValueDemo {
    public static void main(String[] args) {
        Integer[] numbers = {100, 127, 128, 200, 255, 256, -129};

        for (Integer number : numbers) {
            System.out.println(number + " -> " + number.byteValue());
        }
    }
}

How It Works

  1. In range stays the same. 100 and 127 fit in a byte, so they come back unchanged.
  2. One past the top. 128 needs a ninth bit, so it wraps to -128.
  3. 200 and 255. 200 minus 256 gives -56, and 255 minus 256 gives -1, because bit 7 is set and read as the sign.
  4. 256 and -129. 256 has no bits in the low 8 positions, so it gives 0, and -129 wraps the other way to 127.

Example 2 — Split an int into Its Four Bytes

Use shifts and byteValue() to extract each byte of a number, and print them in decimal and hex.

Java
public class IntegerByteValueBytes {
    public static void main(String[] args) {
        Integer number = 0x12345678;

        for (int i = 0; i < 4; i++) {
            Integer shifted = number >> (8 * i);
            byte b = shifted.byteValue();
            System.out.println("byte " + i + ": " + b
                    + " (hex " + Integer.toHexString(b & 0xFF) + ")");
        }
        System.out.println("whole number: " + number);
    }
}

How It Works

  1. Shift, then narrow. Shifting right by 8, 16, or 24 bits brings each byte to the bottom, and byteValue() keeps just that byte.
  2. Lowest byte first. Byte 0 is 0x78, which is 120, followed by 0x56 (86), 0x34 (52), and 0x12 (18).
  3. Masked hex. b & 0xFF widens the byte without sign copying, so the hex digits match the original number.
  4. The whole number. 0x12345678 is 305419896 in decimal.

Example 3 — Get an Unsigned Byte Value Back

Narrow values from 128 to 255, then recover them with & 0xFF.

Java
public class IntegerByteValueSign {
    public static void main(String[] args) {
        Integer[] pixels = {0, 100, 200, 255};

        for (Integer pixel : pixels) {
            byte stored = pixel.byteValue();
            int recovered = stored & 0xFF;
            System.out.println(pixel + " -> stored " + stored + " -> recovered " + recovered);
        }
        System.out.println("limits: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
    }
}

How It Works

  1. A pixel needs 0 to 255. A byte is a convenient 8-bit container, but it is signed.
  2. Small values are fine. 0 and 100 are stored and recovered unchanged.
  3. Large values look negative. 200 is stored as -56 and 255 as -1, because the top bit is the sign bit.
  4. The mask recovers them. stored & 0xFF widens the byte to an int and clears the copied sign bits, so 200 and 255 return.

Frequently Asked Questions

It returns the low 8 bits of the Integer as a primitive byte, which holds numbers from -128 to 127.
A byte is signed. 200 has the bit pattern 11001000, and when the top bit is read as the sign, that is -56.
Use b & 0xFF. It widens the byte to an int and removes the copied sign bits.
No. It keeps the low 8 bits and ignores the rest, so 256 becomes 0.
For an Integer they give the same result. A cast works on a primitive int, byteValue() on an Integer object.

Next: compare()

Compare two int values safely: negative, zero, or positive.

compare() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful