JDK

java.lang.Byteクラスのソースコード

java.lang.Byteクラスはプリミティブ型のbyteのラッパークラスである。最大値,最小値を表すMIN_VALUEとMAX_VALUEはそれぞれリテラルで127,-128と書かれている。

値はbyte型のprivateフィールド valueに保持するよう定義されている。 static initializerですべての値のインスタンスを作成してキャッシュを保持していることがわかる。

decode()メソッドは,Integerクラスのdecode()メソッドを呼び出して,Byte型の範囲であればbyte型にキャスト,Byte型の範囲外の場合はNumberFormatExceptionをスローするようになっている。

shortValue(),intValue(),longValue(),floatValue(),doubleValue()メソッドは,プリミティブ型のbyte値をそれぞれの型へキャストしているだけである。

equals(Object)メソッドは他のクラスと比べるととても簡単である。Byteクラスのインスタンスであれば,==演算子で一致した場合のみtrueを返している。キャッシュを使って同じ値であれば同じオブジェクトを使うように実装されているため,このような比較が可能になっている。

/*
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the “Classpath” exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package java.lang;

import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.misc.VM;

/**
 *
 * The {@code Byte} class wraps a value of primitive type {@code byte}
 * in an object.  An object of type {@code Byte} contains a single
 * field whose type is {@code byte}.
 *
 * <p>In addition, this class provides several methods for converting
 * a {@code byte} to a {@code String} and a {@code String} to a {@code
 * byte}, as well as other constants and methods useful when dealing
 * with a {@code byte}.
 *
 * @author  Nakul Saraiya
 * @author  Joseph D. Darcy
 * @see     java.lang.Number
 * @since   1.1
 */
public final class Byte extends Number implements Comparable<Byte> {

    public static final byte   MIN_VALUE = -128;

    public static final byte   MAX_VALUE = 127;

    @SuppressWarnings(“unchecked”)
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass(“byte”);

    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }

    private static class ByteCache {
        private ByteCache() {}

        static final Byte[] cache;
        static Byte[] archivedCache;

        static {
            final int size = -(-128) + 127 + 1;

            // Load and use the archived cache if it exists
            VM.initializeFromArchive(ByteCache.class);
            if (archivedCache == null || archivedCache.length != size) {
                Byte[] c = new Byte[size];
                byte value = (byte)-128;
                for(int i = 0; i < size; i++) {
                    c[i] = new Byte(value++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
        }
    }

    @HotSpotIntrinsicCandidate
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

    public static byte parseByte(String s, int radix)
        throws NumberFormatException {
        int i = Integer.parseInt(s, radix);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                “Value out of range. Value:\”” + s + “\” Radix:” + radix);
        return (byte)i;
    }

    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    “Value ” + i + ” out of range from input ” + nm);
        return valueOf((byte)i);
    }

    private final byte value;

    @HotSpotIntrinsicCandidate
    public byte byteValue() {
        return value;
    }

    public short shortValue() {
        return (short)value;
    }

    public int intValue() {
        return (int)value;
    }
    
    public long longValue() {
        return (long)value;
    }

    public float floatValue() {
        return (float)value;
    }

    public double doubleValue() {
        return (double)value;
    }

    public String toString() {
        return Integer.toString((int)value);
    }

    @Override
    public int hashCode() {
        return Byte.hashCode(value);
    }

    public static int hashCode(byte value) {
        return (int)value;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    public static int compare(byte x, byte y) {
        return x – y;
    }

    public static int compareUnsigned(byte x, byte y) {
        return Byte.toUnsignedInt(x) – Byte.toUnsignedInt(y);
    }

    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }

    public static final int SIZE = 8;

    public static final int BYTES = SIZE / Byte.SIZE;
}