ZetCode

Java Byte Class

Last modified: April 13, 2025

The java.lang.Byte class is a wrapper class for the primitive type byte. It provides several utility methods for working with byte values, including conversion, comparison, and parsing operations. This class is part of Java's wrapper classes that enable primitive types to be used as objects.

The Byte class is final and immutable, meaning its value cannot be changed after creation. It extends the Number class and implements the Comparable interface. This makes Byte objects compatible with Java collections and numeric operations.

Byte Class Methods

The Byte class provides several static and instance methods for byte operations. Key methods include parseByte, valueOf, byteValue, compare, and toString. These methods enable conversion between strings and bytes, and numeric operations.

public final class Byte extends Number implements Comparable<Byte> {
    public static final byte MIN_VALUE = -128;
    public static final byte MAX_VALUE = 127;
    
    public static byte parseByte(String s) {...}
    public static Byte valueOf(byte b) {...}
    public static Byte valueOf(String s) {...}
    public byte byteValue() {...}
    public static int compare(byte x, byte y) {...}
    public int compareTo(Byte anotherByte) {...}
    public static String toString(byte b) {...}
    public boolean equals(Object obj) {...}
    public int hashCode() {...}
}

The code above shows key methods and constants of the Byte class. The MIN_VALUE and MAX_VALUE constants define the range of byte values (-128 to 127). The class provides various ways to create and manipulate Byte objects.

Creating Byte Objects

Byte objects can be created using constructors or static factory methods. Since Java 9, constructors are deprecated in favor of valueOf methods. This is because valueOf may cache frequently used values for better performance.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Using constructor (deprecated since Java 9)
        Byte byte1 = new Byte((byte) 100);
        
        // Using valueOf methods (recommended)
        Byte byte2 = Byte.valueOf((byte) 100);
        Byte byte3 = Byte.valueOf("100");
        
        // Using autoboxing
        Byte byte4 = 100;
        
        System.out.println("byte1: " + byte1);
        System.out.println("byte2: " + byte2);
        System.out.println("byte3: " + byte3);
        System.out.println("byte4: " + byte4);
        
        // Comparing objects
        System.out.println("byte1 == byte2: " + (byte1 == byte2));
        System.out.println("byte2 == byte3: " + (byte2 == byte3));
        System.out.println("byte3 == byte4: " + (byte3 == byte4));
    }
}

This example demonstrates different ways to create Byte objects. The valueOf method is preferred over constructors. Note that due to caching, small byte values (between -128 and 127) may refer to the same object when using valueOf.

Parsing Byte Values

The Byte class provides methods to parse string representations of byte values. The parseByte method converts a string to a primitive byte, while valueOf returns a Byte object. Both methods can throw NumberFormatException for invalid input.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // Parsing decimal strings
        byte b1 = Byte.parseByte("100");
        Byte b2 = Byte.valueOf("100");
        
        // Parsing hexadecimal strings
        byte b3 = Byte.parseByte("A", 16);
        Byte b4 = Byte.valueOf("7F", 16);
        
        System.out.println("b1: " + b1);
        System.out.println("b2: " + b2);
        System.out.println("b3: " + b3); // 10 in decimal
        System.out.println("b4: " + b4); // 127 in decimal
        
        try {
            // This will throw NumberFormatException
            byte b5 = Byte.parseByte("200");
        } catch (NumberFormatException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

This example shows how to parse byte values from strings. The radix parameter allows parsing hexadecimal (base 16) values. Note that "200" is invalid as it exceeds Byte.MAX_VALUE (127), causing NumberFormatException.

Comparing Byte Values

Byte values can be compared using instance methods or static utility methods. The compareTo method compares two Byte objects, while compare compares primitive bytes. Both return negative, zero, or positive values indicating ordering.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Byte byte1 = 50;
        Byte byte2 = 100;
        byte byte3 = 50;
        
        // Instance comparison
        System.out.println("byte1.compareTo(byte2): " + byte1.compareTo(byte2));
        System.out.println("byte1.compareTo(Byte.valueOf(byte3)): " + 
                         byte1.compareTo(Byte.valueOf(byte3)));
        
        // Static comparison
        System.out.println("Byte.compare(byte1, byte2): " + 
                         Byte.compare(byte1, byte2));
        System.out.println("Byte.compare(byte3, byte1.byteValue()): " + 
                         Byte.compare(byte3, byte1.byteValue()));
        
        // Equality comparison
        System.out.println("byte1.equals(byte2): " + byte1.equals(byte2));
        System.out.println("byte1.equals(Byte.valueOf(byte3)): " + 
                         byte1.equals(Byte.valueOf(byte3)));
    }
}

This example demonstrates different ways to compare byte values. The compareTo and compare methods return -1, 0, or 1 for less than, equal to, or greater than comparisons. The equals method checks for exact value equality between objects.

Converting Byte Values

The Byte class extends Number, providing methods to convert byte values to other primitive numeric types. These include intValue, doubleValue, etc. The toString methods convert bytes to string representations.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        Byte byteValue = 100;
        
        // Converting to other primitive types
        System.out.println("intValue: " + byteValue.intValue());
        System.out.println("doubleValue: " + byteValue.doubleValue());
        System.out.println("floatValue: " + byteValue.floatValue());
        System.out.println("longValue: " + byteValue.longValue());
        System.out.println("shortValue: " + byteValue.shortValue());
        
        // String conversions
        System.out.println("toString: " + byteValue.toString());
        System.out.println("static toString: " + Byte.toString((byte) 100));
        System.out.println("toHexString: " + 
                         Integer.toHexString(byteValue & 0xFF));
    }
}

This example shows various conversion methods available in the Byte class. While Byte provides basic string conversion, for hexadecimal representation we use Integer.toHexString with proper byte masking to avoid sign extension issues.

Byte Constants and Size

The Byte class defines useful constants for the minimum and maximum values a byte can represent. It also provides the SIZE constant representing the number of bits used to store a byte value (always 8 bits in Java).

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        System.out.println("Byte.MIN_VALUE: " + Byte.MIN_VALUE);
        System.out.println("Byte.MAX_VALUE: " + Byte.MAX_VALUE);
        System.out.println("Byte.SIZE: " + Byte.SIZE + " bits");
        System.out.println("Byte.BYTES: " + Byte.BYTES + " bytes");
        
        // Demonstrating byte range
        byte minByte = Byte.MIN_VALUE;
        byte maxByte = Byte.MAX_VALUE;
        
        System.out.println("minByte - 1: " + (minByte - 1));
        System.out.println("maxByte + 1: " + (maxByte + 1));
        
        // Binary representation
        System.out.println("MIN_VALUE binary: " + 
                         Integer.toBinaryString(minByte & 0xFF));
        System.out.println("MAX_VALUE binary: " + 
                         Integer.toBinaryString(maxByte & 0xFF));
    }
}

This example demonstrates Byte class constants and byte size information. Note that arithmetic operations on bytes promote them to int, so overflow doesn't occur in the same way as with pure byte operations. The binary representation shows the actual 8-bit pattern when properly masked.

Byte Hash Code and Equality

The Byte class overrides hashCode and equals from Object. The hashCode returns the byte value itself, while equals compares the wrapped byte values. This ensures proper behavior when using Byte objects in collections.

Main.java
package com.zetcode;

import java.util.HashSet;

public class Main {

    public static void main(String[] args) {
        Byte byte1 = 100;
        Byte byte2 = 100;
        Byte byte3 = 50;
        
        // Hash code examples
        System.out.println("byte1.hashCode(): " + byte1.hashCode());
        System.out.println("byte2.hashCode(): " + byte2.hashCode());
        System.out.println("byte3.hashCode(): " + byte3.hashCode());
        
        // Equality examples
        System.out.println("byte1.equals(byte2): " + byte1.equals(byte2));
        System.out.println("byte1.equals(byte3): " + byte1.equals(byte3));
        
        // Using in HashSet
        HashSet<Byte> byteSet = new HashSet<>();
        byteSet.add(byte1);
        byteSet.add(byte2);
        byteSet.add(byte3);
        
        System.out.println("Set size: " + byteSet.size());
        System.out.println("Set contains 100: " + byteSet.contains((byte) 100));
    }
}

This example demonstrates the hashCode and equals behavior of Byte objects. Identical byte values produce the same hash code and are considered equal. The HashSet example shows proper collection behavior due to these implementations.

Byte Decoding and Encoding

The Byte class can be used in conjunction with character encoding operations. While Java's String uses UTF-16 internally, bytes are often used for ASCII or other single-byte character encodings. This example shows basic byte/char conversion.

Main.java
package com.zetcode;

public class Main {

    public static void main(String[] args) {
        // ASCII character to byte
        byte letterA = (byte) 'A';
        Byte letterB = (byte) 'B';
        
        System.out.println("letterA: " + letterA);
        System.out.println("letterB: " + letterB);
        
        // Byte to character
        char aChar = (char) letterA.byteValue();
        char bChar = (char) letterB.byteValue();
        
        System.out.println("aChar: " + aChar);
        System.out.println("bChar: " + bChar);
        
        // String to bytes and back
        String text = "Hello";
        byte[] bytes = text.getBytes();
        
        System.out.print("Bytes: ");
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();
        
        String reconstructed = new String(bytes);
        System.out.println("Reconstructed: " + reconstructed);
    }
}

This example shows basic conversion between bytes and characters. Note that this simple approach works for ASCII characters (0-127) but may not handle extended character sets properly. For full character encoding support, use appropriate Charset objects with String.getBytes() and String constructors.

Source

Java Byte Class Documentation

In this article, we've covered all major aspects of the Java Byte class with practical examples. The Byte wrapper class is essential when byte values need to be treated as objects or when utility methods for byte manipulation are required.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.