Java : Short with Examples

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


Summary

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

Class diagram

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

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

Fields

static final int BYTES

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

System.out.println(Short.BYTES); // 2

static final short MAX_VALUE

A constant holding the maximum value a short can have, 215-1.

System.out.println(Short.MAX_VALUE); // 32767
System.out.println("0x%x".formatted(Short.MAX_VALUE)); // 0x7fff

static final short MIN_VALUE

A constant holding the minimum value a short can have, -215.

System.out.println(Short.MIN_VALUE); // -32768
System.out.println("0x%x".formatted(Short.MIN_VALUE)); // 0x8000

static final int SIZE

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

System.out.println(Short.SIZE); // 16

static final Class<Short> TYPE

The Class instance representing the primitive type short.

System.out.println(Short.TYPE.getSimpleName()); // "short"
System.out.println(Short.TYPE.isPrimitive()); // true

Constructors

Short (short 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(short s) instead.

Short (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 Short as a byte after a narrowing primitive conversion.

final var ret1 = Short.valueOf((short) 0).byteValue();
System.out.println(ret1); // 0

final var ret2 = Short.valueOf((short) 123).byteValue();
System.out.println(ret2); // 123

final var ret3 = Short.valueOf((short) -45).byteValue();
System.out.println(ret3); // -45
final var ret1 = Short.valueOf(Byte.MAX_VALUE).byteValue();
System.out.println(ret1); // 127

final var ret2 = Short.valueOf(Byte.MIN_VALUE).byteValue();
System.out.println(ret2); // -128
final var ret1 = Short.valueOf((short) 127).byteValue();
System.out.println(ret1); // 127

final var ret3 = Short.valueOf((short) 128).byteValue();
System.out.println(ret3); // -128

final var ret4 = Short.valueOf((short) 129).byteValue();
System.out.println(ret4); // -127
final var ret1 = Short.valueOf((short) -128).byteValue();
System.out.println(ret1); // -128

final var ret3 = Short.valueOf((short) -129).byteValue();
System.out.println(ret3); // 127

final var ret4 = Short.valueOf((short) -130).byteValue();
System.out.println(ret4); // 126

static int compare (short x, short y)

Compares two short values numerically.

System.out.println(Short.compare((short) 0, (short) 0)); // 0
System.out.println(Short.compare((short) 0, (short) 1)); // -1
System.out.println(Short.compare((short) 1, (short) 0)); // 1
System.out.println(Short.compare((short) 1, (short) -2)); // 3
System.out.println(Short.compare((short) -4, (short) -2)); // -2
System.out.println(Short.compare((short) 1000, (short) -1000)); // 2000

int compareTo (Short anotherShort)

Compares two Short objects numerically.

final var value1 = Short.valueOf((short) 0);
final var value2 = Short.valueOf((short) 0);
System.out.println(value1.compareTo(value2)); // 0
final var value1 = Short.valueOf((short) 0);
final var value2 = Short.valueOf((short) 1);
System.out.println(value1.compareTo(value2)); // -1
final var value1 = Short.valueOf((short) 1);
final var value2 = Short.valueOf((short) 0);
System.out.println(value1.compareTo(value2)); // 1
final var value1 = Short.valueOf((short) 1);
final var value2 = Short.valueOf((short) -2);
System.out.println(value1.compareTo(value2)); // 3
final var value1 = Short.valueOf((short) -4);
final var value2 = Short.valueOf((short) -2);
System.out.println(value1.compareTo(value2)); // -2
final var value1 = Short.valueOf(Short.MAX_VALUE);
final var value2 = Short.valueOf(Short.MIN_VALUE);
System.out.println(value1.compareTo(value2)); // 65535

static int compareUnsigned (short x, short y)

Compares two short values numerically treating the values as unsigned.

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

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

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

System.out.println(Short.compare(value1, value2)); // -2
System.out.println(Short.compareUnsigned(value1, value2)); // 65534
final short value1 = -1;
final short value2 = -2;
System.out.println("0x%x".formatted(value1)); // "0xffff"
System.out.println("0x%x".formatted(value2)); // "0xfffe"

System.out.println(Short.compare(value1, value2)); // 1
System.out.println(Short.compareUnsigned(value1, value2)); // 1
final short value1 = Short.MAX_VALUE;
final short value2 = Short.MIN_VALUE;
System.out.println("0x%x".formatted(value1)); // "0x7fff"
System.out.println("0x%x".formatted(value2)); // "0x8000"

System.out.println(Short.compare(value1, value2)); // 65535
System.out.println(Short.compareUnsigned(value1, value2)); // -1

static Short decode (String nm)

Decodes a String into a Short.

System.out.println(Short.decode("0")); // 0
System.out.println(Short.decode("123")); // 123
System.out.println(Short.decode("+456")); // 456
System.out.println(Short.decode("-789")); // -789

//Short.decode("xyz"); // NumberFormatException
// Hexadecimal
final var ret1 = Short.decode("0xff");
System.out.println(ret1); // 255
System.out.println("%x".formatted(ret1)); // "ff"

final var ret2 = Short.decode("-0xff");
System.out.println(ret2); // -255
System.out.println("%x".formatted(ret2)); // "ff01"

final var ret3 = Short.decode("#abc");
System.out.println(ret3); // 2748
System.out.println("%x".formatted(ret3)); // "abc"

final var ret4 = Short.decode("0X7fff");
System.out.println(ret4); // 32767
System.out.println("%x".formatted(ret4)); // "7fff"
// Octal
final var ret1 = Short.decode("01234");
System.out.println(ret1); // 668
System.out.println("%o".formatted(ret1)); // "1234"

final var ret2 = Short.decode("-01234");
System.out.println(ret2); // -668
System.out.println("%o".formatted(ret2)); // "176544"

Optional<DynamicConstantDesc<Short>> describeConstable ()

Returns an Optional containing the nominal descriptor for this instance.

final var ret = Short.valueOf((short) 123).describeConstable();

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

double doubleValue ()

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

final var value = Short.valueOf((short) 1234).doubleValue();
System.out.println(value); // 1234.0
final var value = Short.valueOf((short) -4567).doubleValue();
System.out.println(value); // -4567.0
final var value = Short.valueOf(Short.MAX_VALUE).doubleValue();
System.out.println(value); // 32767.0
final var value = Short.valueOf(Short.MIN_VALUE).doubleValue();
System.out.println(value); // -32768.0

boolean equals (Object obj)

Compares this object to the specified object.

final var value1 = Short.valueOf((short) 100);
final var value2 = Short.valueOf((short) 100);
final var value3 = Short.valueOf((short) 999);

System.out.println(value1.equals((short) 100)); // true
System.out.println(value1.equals(value2)); // true
System.out.println(value1.equals(value3)); // false
final var value1 = Short.valueOf((short) 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 Short as a float after a widening primitive conversion.

final var value = Short.valueOf((short) 1234).floatValue();
System.out.println(value); // 1234.0
final var value = Short.valueOf((short) -4567).floatValue();
System.out.println(value); // -4567.0
final var value = Short.valueOf(Short.MAX_VALUE).floatValue();
System.out.println(value); // 32767.0
final var value = Short.valueOf(Short.MIN_VALUE).floatValue();
System.out.println(value); // -32768.0

int hashCode ()

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

System.out.println(Short.valueOf((short) 0).hashCode()); // 0
System.out.println(Short.valueOf((short) 1).hashCode()); // 1
System.out.println(Short.valueOf((short) 2).hashCode()); // 2
System.out.println(Short.valueOf((short) 3456).hashCode()); // 3456
System.out.println(Short.valueOf((short) -7890).hashCode()); // -7890

System.out.println(Short.valueOf(Short.MAX_VALUE).hashCode()); // 32767
System.out.println(Short.valueOf(Short.MIN_VALUE).hashCode()); // -32768

static int hashCode (short value)

Returns a hash code for a short value; compatible with Short.hashCode().

System.out.println(Short.hashCode((short) 0)); // 0
System.out.println(Short.hashCode((short) 1)); // 1
System.out.println(Short.hashCode((short) 2)); // 2
System.out.println(Short.hashCode((short) 3456)); // 3456
System.out.println(Short.hashCode((short) -7890)); // -7890

System.out.println(Short.hashCode(Short.MAX_VALUE)); // 32767
System.out.println(Short.hashCode(Short.MIN_VALUE)); // -32768

int intValue ()

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

final var value = Short.valueOf((short) 1234).intValue();
System.out.println(value); // 1234
final var value = Short.valueOf((short) -4567).intValue();
System.out.println(value); // -4567
final var value = Short.valueOf(Short.MAX_VALUE).intValue();
System.out.println(value); // 32767
final var value = Short.valueOf(Short.MIN_VALUE).intValue();
System.out.println(value); // -32768

long longValue ()

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

final var value = Short.valueOf((short) 1234).longValue();
System.out.println(value); // 1234
final var value = Short.valueOf((short) -4567).longValue();
System.out.println(value); // -4567
final var value = Short.valueOf(Short.MAX_VALUE).longValue();
System.out.println(value); // 32767
final var value = Short.valueOf(Short.MIN_VALUE).longValue();
System.out.println(value); // -32768

static short parseShort (String s)

Parses the string argument as a signed decimal short.

System.out.println(Short.parseShort("0")); // 0
System.out.println(Short.parseShort("123")); // 123
System.out.println(Short.parseShort("+456")); // 456
System.out.println(Short.parseShort("-789")); // -789

System.out.println(Short.parseShort("32767")); // 32767
//Short.parseShort("32768"); // NumberFormatException

System.out.println(Short.parseShort("-32768")); // -32768
//Short.parseShort("-32769"); // NumberFormatException

static short parseShort (String s, int radix)

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

System.out.println(Short.parseShort("0", 10)); // 0
System.out.println(Short.parseShort("473", 10)); // 473
System.out.println(Short.parseShort("+42", 10)); // 42
System.out.println(Short.parseShort("-0", 10)); // 0
System.out.println(Short.parseShort("-FF", 16)); // -255
System.out.println(Short.parseShort("1100110", 2)); // 102
System.out.println(Short.parseShort("32767", 10)); // 32767
System.out.println(Short.parseShort("-32768", 10)); // -32768

//Short.parseShort("32768", 10); // NumberFormatException
//Short.parseShort("99", 8); // NumberFormatException
//Short.parseShort("Kon", 10); // NumberFormatException

System.out.println(Short.parseShort("Kon", 27)); // 15251

static short reverseBytes (short i)

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

final short value = 0x0;
System.out.println("0x%x".formatted(value)); // "0x0"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x0"
final short value = 0x1;
System.out.println("0x%x".formatted(value)); // "0x1"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x100"
final short value = 0x2;
System.out.println("0x%x".formatted(value)); // "0x2"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x200"
final short value = 0xf;
System.out.println("0x%x".formatted(value)); // "0xf"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0xf00"
final short value = 0x10;
System.out.println("0x%x".formatted(value)); // "0x10"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x1000"
final short value = 0x11;
System.out.println("0x%x".formatted(value)); // "0x11"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x1100"
final short value = 0x12;
System.out.println("0x%x".formatted(value)); // "0x12"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x1200"
final short value = 0x1f;
System.out.println("0x%x".formatted(value)); // "0x1f"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x1f00"
final short value = 0x100;
System.out.println("0x%x".formatted(value)); // "0x100"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x1"
final short value = Short.MAX_VALUE;
System.out.println("0x%x".formatted(value)); // "0x7fff"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0xff7f"
final short value = Short.MIN_VALUE;
System.out.println("0x%x".formatted(value)); // "0x8000"

final var ret = Short.reverseBytes(value);
System.out.println("0x%x".formatted(ret)); // "0x80"

short shortValue ()

Returns the value of this Short as a short.

final var value = Short.valueOf((short) 1234).shortValue();
System.out.println(value); // 1234
final var value = Short.valueOf((short) -4567).shortValue();
System.out.println(value); // -4567
final var value = Short.valueOf(Short.MAX_VALUE).shortValue();
System.out.println(value); // 32767
final var value = Short.valueOf(Short.MIN_VALUE).shortValue();
System.out.println(value); // -32768

String toString ()

Returns a String object representing this Short's value.

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

final var ret2 = Short.valueOf((short) -456).toString();
System.out.println(ret2); // "-456"

static String toString (short s)

Returns a new String object representing the specified short.

final var ret1 = Short.toString((short) 123);
System.out.println(ret1); // "123"

final var ret2 = Short.toString((short) -456);
System.out.println(ret2); // "-456"

System.out.println(Short.toString(Short.MAX_VALUE)); // "32767"
System.out.println(Short.toString(Short.MIN_VALUE)); // "-32768"

static int toUnsignedInt (short x)

Converts the argument to an int by an unsigned conversion.

final short x = 255;
System.out.println("%x".formatted(x)); // "ff"

final var ret = Short.toUnsignedInt(x);
System.out.println(ret); // 255
final short x = -1;
System.out.println("%x".formatted(x)); // "ffff"

final var ret = Short.toUnsignedInt(x);
System.out.println(ret); // 65535

static long toUnsignedLong (short x)

Converts the argument to a long by an unsigned conversion.

final short x = 255;
System.out.println("%x".formatted(x)); // "ff"

final var ret = Short.toUnsignedLong(x);
System.out.println(ret); // 255
final short x = -1;
System.out.println("%x".formatted(x)); // "ffff"

final var ret = Short.toUnsignedLong(x);
System.out.println(ret); // 65535

static Short valueOf (short s)

Returns a Short instance representing the specified short value.

System.out.println(Short.valueOf((short) 0)); // 0
System.out.println(Short.valueOf((short) 1)); // 1
System.out.println(Short.valueOf((short) 2)); // 2
System.out.println(Short.valueOf((short) 100)); // 100
System.out.println(Short.valueOf((short) 9999)); // 9999

System.out.println(Short.valueOf((short) -1)); // -1
System.out.println(Short.valueOf((short) -2)); // -2
System.out.println(Short.valueOf((short) -3)); // -3

System.out.println(Short.valueOf(Short.MAX_VALUE)); // 32767
System.out.println(Short.valueOf(Short.MIN_VALUE)); // -32768

static Short valueOf (String s)

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

System.out.println(Short.valueOf("0")); // 0
System.out.println(Short.valueOf("123")); // 123
System.out.println(Short.valueOf("+456")); // 456
System.out.println(Short.valueOf("-789")); // -789

System.out.println(Short.valueOf("32767")); // 32767
//Short.valueOf("32768"); // NumberFormatException

System.out.println(Short.valueOf("-32768")); // -32768
//Short.valueOf("-32769"); // NumberFormatException

static Short valueOf (String s, int radix)

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

System.out.println(Short.valueOf("0", 10)); // 0
System.out.println(Short.valueOf("473", 10)); // 473
System.out.println(Short.valueOf("+42", 10)); // 42
System.out.println(Short.valueOf("-0", 10)); // 0
System.out.println(Short.valueOf("-FF", 16)); // -255
System.out.println(Short.valueOf("1100110", 2)); // 102
System.out.println(Short.valueOf("32767", 10)); // 32767
System.out.println(Short.valueOf("-32768", 10)); // -32768

//Short.valueOf("32768", 10); // NumberFormatException
//Short.valueOf("99", 8); // NumberFormatException
//Short.valueOf("Kon", 10); // NumberFormatException

System.out.println(Short.valueOf("Kon", 27)); // 15251

Related posts

To top of page