Java : Byte with Examples
Byte (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Byte methods.
Summary
// 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
System.out.println(Byte.BYTES); // 1
static final byte MAX_VALUE
System.out.println(Byte.MAX_VALUE); // 127
System.out.println("0x%x".formatted(Byte.MAX_VALUE)); // 0x7f
static final byte MIN_VALUE
System.out.println(Byte.MIN_VALUE); // -128
System.out.println("0x%x".formatted(Byte.MIN_VALUE)); // 0x80
static final int SIZE
System.out.println(Byte.SIZE); // 8
static final Class<Byte> TYPE
System.out.println(Byte.TYPE.getSimpleName()); // "byte"
System.out.println(Byte.TYPE.isPrimitive()); // true
Constructors
Byte (byte value)
Deprecated. Use valueOf(byte b) instead.
Byte (String s)
Deprecated. Use valueOf(String s) instead.
Methods
byte byteValue ()
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)
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)
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)
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)
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 ()
final var ret = Byte.valueOf((byte) 123).describeConstable();
// Optional[DynamicConstantDesc[ConstantBootstraps::explicitCast(123)byte]]
System.out.println(ret);
double doubleValue ()
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)
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 ()
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 ()
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)
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 ()
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 ()
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)
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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