1. 程式人生 > 其它 >Java 原始碼 - Byte

Java 原始碼 - Byte

介紹

The Byte class wraps a value of primitive type byte in an object.

示例

public class Test {
  public static void main(String[] args) {
    Byte c = new Byte("120");
    System.out.println(c.toString());
  }
}

原始碼

public final class Byte extends Number implements Comparable<Byte> {

  /**
   * -2<sup>7</sup>.
   */
  public static final byte MIN_VALUE = -128;

  /**
   * 2<sup>7</sup>-1.
   */
  public static final byte MAX_VALUE = 127;

  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.");
    return (byte)i;
  }

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

  private final byte value;

  public Byte(byte value) {
    this.value = value;
  }
  
  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);
  }

  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 static int compare(byte x, byte y) {
    return x - y;
  }
}