Java : Byte with Examples

Byte (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Byte methods.


Summary

The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

Class diagram

// Compile error.
final var list = new ArrayList<byte>();
// OK.
final var list = new ArrayList<Byte>();
list.add((byte) 1);
list.add((byte) 2);
list.add((byte) 3);

System.out.println(list); // [1, 2, 3]

Fields

static final int BYTES

The number of bytes used to represent a byte value in two's complement binary form.

System.out.println(Byte.BYTES); // 1

static final byte MAX_VALUE

A constant holding the maximum value a byte can have, 27-1.

System.out.println(Byte.MAX_VALUE); // 127
System.out.println("0x%x".formatted(Byte.MAX_VALUE)); // 0x7f

static final byte MIN_VALUE

A constant holding the minimum value a byte can have, -27.

System.out.println(Byte.MIN_VALUE); // -128
System.out.println("0x%x".formatted(Byte.MIN_VALUE)); // 0x80

static final int SIZE

The number of bits used to represent a byte value in two's complement binary form.

System.out.println(Byte.SIZE); // 8

static final Class<Byte> TYPE

The Class instance representing the primitive type byte.

System.out.println(Byte.TYPE.getSimpleName()); // "byte"
System.out.println(Byte.TYPE.isPrimitive()); // true

Constructors

Byte (byte value)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(byte b) instead.

Byte (String s)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(String s) instead.

Methods

byte byteValue ()

Returns the value of this Byte as a byte.

final var value = Byte.valueOf((byte) 123).byteValue();
System.out.println(value); // 123
final var value = Byte.valueOf((byte) -45).byteValue();
System.out.println(value); // -45

static int compare (byte x, byte y)

Compares two byte values numerically.

System.out.println(Byte.compare((byte) 0, (byte) 0)); // 0
System.out.println(Byte.compare((byte) 0, (byte) 1)); // -1
System.out.println(Byte.compare((byte) 1, (byte) 0)); // 1
System.out.println(Byte.compare((byte) 1, (byte) -2)); // 3
System.out.println(Byte.compare((byte) -100, (byte) -2)); // -98

int compareTo (Byte anotherByte)

Compares two Byte objects numerically.

final var value1 = Byte.valueOf((byte) 0);
final var value2 = Byte.valueOf((byte) 0);
System.out.println(value1.compareTo(value2)); // 0
final var value1 = Byte.valueOf((byte) 0);
final var value2 = Byte.valueOf((byte) 1);
System.out.println(value1.compareTo(value2)); // -1
final var value1 = Byte.valueOf((byte) 1);
final var value2 = Byte.valueOf((byte) 0);
System.out.println(value1.compareTo(value2)); // 1
final var value1 = Byte.valueOf((byte) 1);
final var value2 = Byte.valueOf((byte) -2);
System.out.println(value1.compareTo(value2)); // 3
final var value1 = Byte.valueOf((byte) -100);
final var value2 = Byte.valueOf((byte) -2);
System.out.println(value1.compareTo(value2)); // -98

static int compareUnsigned (byte x, byte y)

Compares two byte values numerically treating the values as unsigned.

final byte value1 = 0;
final byte value2 = 0;
System.out.println("0x%x".formatted(value1)); // "0x0"
System.out.println("0x%x".formatted(value2)); // "0x0"

System.out.println(Byte.compare(value1, value2)); // 0
System.out.println(Byte.compareUnsigned(value1, value2)); // 0
final byte value1 = 5;
final byte value2 = 10;
System.out.println("0x%x".formatted(value1)); // "0x5"
System.out.println("0x%x".formatted(value2)); // "0xa"

System.out.println(Byte.compare(value1, value2)); // -5
System.out.println(Byte.compareUnsigned(value1, value2)); // -5
final byte value1 = -1;
final byte value2 = 1;
System.out.println("0x%x".formatted(value1)); // "0xff"
System.out.println("0x%x".formatted(value2)); // "0x1"

System.out.println(Byte.compare(value1, value2)); // -2
System.out.println(Byte.compareUnsigned(value1, value2)); // 254
final byte value1 = -1;
final byte value2 = -2;
System.out.println("0x%x".formatted(value1)); // "0xff"
System.out.println("0x%x".formatted(value2)); // "0xfe"

System.out.println(Byte.compare(value1, value2)); // 1
System.out.println(Byte.compareUnsigned(value1, value2)); // 1
final byte value1 = Byte.MAX_VALUE;
final byte value2 = Byte.MIN_VALUE;
System.out.println("0x%x".formatted(value1)); // "0x7f"
System.out.println("0x%x".formatted(value2)); // "0x80"

System.out.println(Byte.compare(value1, value2)); // 255
System.out.println(Byte.compareUnsigned(value1, value2)); // -1

static Byte decode (String nm)

Decodes a String into a Byte.

System.out.println(Byte.decode("0")); // 0
System.out.println(Byte.decode("12")); // 12
System.out.println(Byte.decode("+45")); // 45
System.out.println(Byte.decode("-78")); // -78

//Byte.decode("xyz"); // NumberFormatException
// Hexadecimal
final var ret1 = Byte.decode("0xf");
System.out.println(ret1); // 15
System.out.println("%x".formatted(ret1)); // "f"

final var ret2 = Byte.decode("-0xf");
System.out.println(ret2); // -15
System.out.println("%x".formatted(ret2)); // "f1"

final var ret3 = Byte.decode("#5a");
System.out.println(ret3); // 90
System.out.println("%x".formatted(ret3)); // "5a"

final var ret4 = Byte.decode("0X7f");
System.out.println(ret4); // 127
System.out.println("%x".formatted(ret4)); // "7f"
// Octal
final var ret1 = Byte.decode("012");
System.out.println(ret1); // 10
System.out.println("%o".formatted(ret1)); // "12"

final var ret2 = Byte.decode("-012");
System.out.println(ret2); // -10
System.out.println("%o".formatted(ret2)); // "366"

Optional<DynamicConstantDesc<Byte>> describeConstable ()

Returns an Optional containing the nominal descriptor for this instance.

final var ret = Byte.valueOf((byte) 123).describeConstable();

// Optional[DynamicConstantDesc[ConstantBootstraps::explicitCast(123)byte]]
System.out.println(ret);

double doubleValue ()

Returns the value of this Byte as a double after a widening primitive conversion.

final var value = Byte.valueOf((byte) 12).doubleValue();
System.out.println(value); // 12.0
final var value = Byte.valueOf((byte) -34).doubleValue();
System.out.println(value); // -34.0
final var value = Byte.valueOf(Byte.MAX_VALUE).doubleValue();
System.out.println(value); // 127.0
final var value = Byte.valueOf(Byte.MIN_VALUE).doubleValue();
System.out.println(value); // -128.0

boolean equals (Object obj)

Compares this object to the specified object.

final var value1 = Byte.valueOf((byte) 100);
final var value2 = Byte.valueOf((byte) 100);
final var value3 = Byte.valueOf((byte) -99);

System.out.println(value1.equals((byte) 100)); // true
System.out.println(value1.equals(value2)); // true
System.out.println(value1.equals(value3)); // false
final var value1 = Byte.valueOf((byte) 100);
final var value2 = Integer.valueOf(100);

System.out.println(value1.equals(100)); // false
System.out.println(value1.equals(value2)); // false

float floatValue ()

Returns the value of this Byte as a float after a widening primitive conversion.

final var value = Byte.valueOf((byte) 12).floatValue();
System.out.println(value); // 12.0
final var value = Byte.valueOf((byte) -34).floatValue();
System.out.println(value); // -34.0
final var value = Byte.valueOf(Byte.MAX_VALUE).floatValue();
System.out.println(value); // 127.0
final var value = Byte.valueOf(Byte.MIN_VALUE).floatValue();
System.out.println(value); // -128.0

int hashCode ()

Returns a hash code for this Byte; equal to the result of invoking intValue().

System.out.println(Byte.valueOf((byte) 0).hashCode()); // 0
System.out.println(Byte.valueOf((byte) 1).hashCode()); // 1
System.out.println(Byte.valueOf((byte) 2).hashCode()); // 2
System.out.println(Byte.valueOf((byte) -45).hashCode()); // -45

System.out.println(Byte.valueOf(Byte.MAX_VALUE).hashCode()); // 127
System.out.println(Byte.valueOf(Byte.MIN_VALUE).hashCode()); // -128

static int hashCode (byte value)

Returns a hash code for a byte value; compatible with Byte.hashCode().

System.out.println(Byte.hashCode((byte) 0)); // 0
System.out.println(Byte.hashCode((byte) 1)); // 1
System.out.println(Byte.hashCode((byte) 2)); // 2
System.out.println(Byte.hashCode((byte) -45)); // -45

System.out.println(Byte.hashCode(Byte.MAX_VALUE)); // 127
System.out.println(Byte.hashCode(Byte.MIN_VALUE)); // -128

int intValue ()

Returns the value of this Byte as an int after a widening primitive conversion.

final var value = Byte.valueOf((byte) 12).intValue();
System.out.println(value); // 12
final var value = Byte.valueOf((byte) -34).intValue();
System.out.println(value); // -34
final var value = Byte.valueOf(Byte.MAX_VALUE).intValue();
System.out.println(value); // 127
final var value = Byte.valueOf(Byte.MIN_VALUE).intValue();
System.out.println(value); // -128

long longValue ()

Returns the value of this Byte as a long after a widening primitive conversion.

final var value = Byte.valueOf((byte) 12).longValue();
System.out.println(value); // 12
final var value = Byte.valueOf((byte) -34).longValue();
System.out.println(value); // -34
final var value = Byte.valueOf(Byte.MAX_VALUE).longValue();
System.out.println(value); // 127
final var value = Byte.valueOf(Byte.MIN_VALUE).longValue();
System.out.println(value); // -128

static byte parseByte (String s)

Parses the string argument as a signed decimal byte.

System.out.println(Byte.parseByte("0")); // 0
System.out.println(Byte.parseByte("123")); // 123
System.out.println(Byte.parseByte("+45")); // 45
System.out.println(Byte.parseByte("-78")); // -78

System.out.println(Byte.parseByte("127")); // 127
//Byte.parseByte("128"); // NumberFormatException

System.out.println(Byte.parseByte("-128")); // -128
//Byte.parseByte("-129"); // NumberFormatException

static byte parseByte (String s, int radix)

Parses the string argument as a signed byte in the radix specified by the second argument.

System.out.println(Byte.parseByte("0", 10)); // 0
System.out.println(Byte.parseByte("12", 10)); // 12
System.out.println(Byte.parseByte("+42", 10)); // 42
System.out.println(Byte.parseByte("-0", 10)); // 0
System.out.println(Byte.parseByte("-F", 16)); // -15
System.out.println(Byte.parseByte("1100110", 2)); // 102
System.out.println(Byte.parseByte("127", 10)); // 127
System.out.println(Byte.parseByte("-128", 10)); // -128

//Byte.parseByte("128", 10); // NumberFormatException
//Byte.parseByte("99", 8); // NumberFormatException
//Byte.parseByte("1K", 10); // NumberFormatException

System.out.println(Byte.parseByte("1K", 27)); // 47

short shortValue ()

Returns the value of this Byte as a short after a widening primitive conversion.

final var value = Byte.valueOf((byte) 12).shortValue();
System.out.println(value); // 12
final var value = Byte.valueOf((byte) -34).shortValue();
System.out.println(value); // -34
final var value = Byte.valueOf(Byte.MAX_VALUE).shortValue();
System.out.println(value); // 127
final var value = Byte.valueOf(Byte.MIN_VALUE).shortValue();
System.out.println(value); // -128

String toString ()

Returns a String object representing this Byte's value.

final var ret1 = Byte.valueOf((byte) 123).toString();
System.out.println(ret1); // "123"

final var ret2 = Byte.valueOf((byte) -45).toString();
System.out.println(ret2); // "-45"

static String toString (byte b)

Returns a new String object representing the specified byte.

final var ret1 = Byte.toString((byte) 123);
System.out.println(ret1); // "123"

final var ret2 = Byte.toString((byte) -45);
System.out.println(ret2); // "-45"

System.out.println(Byte.toString(Byte.MAX_VALUE)); // "127"
System.out.println(Byte.toString(Byte.MIN_VALUE)); // "-128"

static int toUnsignedInt (byte x)

Converts the argument to an int by an unsigned conversion.

final byte x = 15;
System.out.println("%x".formatted(x)); // "f"

final var ret = Byte.toUnsignedInt(x);
System.out.println(ret); // 15
final byte x = -1;
System.out.println("%x".formatted(x)); // "ff"

final var ret = Byte.toUnsignedInt(x);
System.out.println(ret); // 255

static long toUnsignedLong (byte x)

Converts the argument to a long by an unsigned conversion.

final byte x = 15;
System.out.println("%x".formatted(x)); // "f"

final var ret = Byte.toUnsignedLong(x);
System.out.println(ret); // 15
final byte x = -1;
System.out.println("%x".formatted(x)); // "ff"

final var ret = Byte.toUnsignedLong(x);
System.out.println(ret); // 255

static Byte valueOf (byte b)

Returns a Byte instance representing the specified byte value.

System.out.println(Byte.valueOf((byte) 0)); // 0
System.out.println(Byte.valueOf((byte) 1)); // 1
System.out.println(Byte.valueOf((byte) 2)); // 2
System.out.println(Byte.valueOf((byte) 100)); // 100

System.out.println(Byte.valueOf((byte) -1)); // -1
System.out.println(Byte.valueOf((byte) -2)); // -2
System.out.println(Byte.valueOf((byte) -3)); // -3

System.out.println(Byte.valueOf(Byte.MAX_VALUE)); // 127
System.out.println(Byte.valueOf(Byte.MIN_VALUE)); // -128

static Byte valueOf (String s)

Returns a Byte object holding the value given by the specified String.

System.out.println(Byte.valueOf("0")); // 0
System.out.println(Byte.valueOf("123")); // 123
System.out.println(Byte.valueOf("+45")); // 45
System.out.println(Byte.valueOf("-78")); // -78

System.out.println(Byte.valueOf("127")); // 127
//Byte.valueOf("128"); // NumberFormatException

System.out.println(Byte.parseByte("-128")); // -128
//Byte.valueOf("-129"); // NumberFormatException

static Byte valueOf (String s, int radix)

Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.

System.out.println(Byte.valueOf("0", 10)); // 0
System.out.println(Byte.valueOf("12", 10)); // 12
System.out.println(Byte.valueOf("+42", 10)); // 42
System.out.println(Byte.valueOf("-0", 10)); // 0
System.out.println(Byte.valueOf("-F", 16)); // -15
System.out.println(Byte.valueOf("1100110", 2)); // 102
System.out.println(Byte.valueOf("127", 10)); // 127
System.out.println(Byte.valueOf("-128", 10)); // -128

//Byte.valueOf("128", 10); // NumberFormatException
//Byte.valueOf("99", 8); // NumberFormatException
//Byte.valueOf("1K", 10); // NumberFormatException

System.out.println(Byte.valueOf("1K", 27)); // 47

Related posts

To top of page