Java : Short - API使用例
Short (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
Short は、プリミティブ型 short のラッパークラスです。
short は Object ではないので、そのままでは List などのコレクションに追加できません。
そんなときは、 Shortクラスを使って short をラップすると追加が可能となります。
// コンパイルエラー
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]
他には、
- short の最大値(Short.MAX_VALUE)、最小値(Short.MIN_VALUE)
- 文字列から short への変換 (Short.parseShort)
などなど、便利なメソッドがそろっています。
関連記事:プリミティブ型 (基本データ型)
フィールド
static final int BYTES
System.out.println(Short.BYTES); // 2
static final short MAX_VALUE
System.out.println(Short.MAX_VALUE); // 32767
System.out.println("0x%x".formatted(Short.MAX_VALUE)); // 0x7fff
static final short MIN_VALUE
System.out.println(Short.MIN_VALUE); // -32768
System.out.println("0x%x".formatted(Short.MIN_VALUE)); // 0x8000
static final int SIZE
System.out.println(Short.SIZE); // 16
static final Class<Short> TYPE
System.out.println(Short.TYPE.getSimpleName()); // "short"
System.out.println(Short.TYPE.isPrimitive()); // true
コンストラクタ
Short (short value)
非推奨 です。
代わりに valueOf(short s) を使います。
Short (String s)
非推奨 です。
代わりに valueOf(String s) を使います。
メソッド
byte byteValue ()
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)
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)
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)
"符号なしとして比較" について補足
-
short のマイナス値は最上位ビットを 1 (0x8000)として判断しています。
その最上位ビットをマイナス値の判定としては使わずに、そのままプラス値として計算します。符号なし表現でイメージしやすいのは、16進数表記でしょうか。
例えば -1 は 0xffff、1 は 0x1 となります。
これを compareUnsigned で比較すると、-1 (0xffff) のほうが大きいとみなされます。
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)
// 10進数
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
// 16進数
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"
// 8進数
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 ()
final var ret = Short.valueOf((short) 123).describeConstable();
// Optional[DynamicConstantDesc[ConstantBootstraps::explicitCast(123)short]]
System.out.println(ret);
double doubleValue ()
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)
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);
// 型が違う場合はfalse
System.out.println(value1.equals(100)); // false
System.out.println(value1.equals(value2)); // false
float floatValue ()
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 ()
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)
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 ()
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 ()
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)
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)
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)
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 ()
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 ()
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)
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)
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)
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)
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)
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)
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