* All files: Updated copyright to reflect Cygnus purchase.
[official-gcc.git] / libjava / java / lang / Byte.java
blobc4cc585cbb8f840d6f4929c291c9f623cf447ba4
1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package java.lang;
11 /**
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date April 17, 1998.
15 /* Written using "Java Class Libraries", 2nd edition, plus online
16 * API docs for JDK 1.2 beta from http://www.javasoft.com.
17 * Status: Believed complete and correct.
18 * Includes JDK 1.2 methods.
21 public final class Byte extends Number implements Comparable
23 byte value;
25 public final static byte MIN_VALUE = -128;
26 public final static byte MAX_VALUE = 127;
28 // This initialization is seemingly circular, but it is accepted
29 // by javac, and is handled specially by gcc.
30 public static final Class TYPE = byte.class;
32 public Byte(byte value)
34 this.value = value;
37 public Byte(String str)
38 throws NumberFormatException
40 this.value = parseByte(str, 10);
43 public byte byteValue()
45 return value;
48 public short shortValue()
50 return value;
53 public int intValue()
55 return value;
58 public long longValue ()
60 return value;
63 public float floatValue ()
65 return (float) value;
68 public double doubleValue ()
70 return (double) value;
73 public static Byte decode(String str)
74 throws NumberFormatException
76 int i = (Integer.decode(str)).intValue();
77 if (i < MIN_VALUE || i > MAX_VALUE)
78 throw new NumberFormatException();
79 return new Byte((byte) i);
82 public static byte parseByte(String str, int radix)
83 throws NumberFormatException
85 int i = Integer.parseInt(str, radix);
86 if (i < MIN_VALUE || i > MAX_VALUE)
87 throw new NumberFormatException();
88 return (byte) i;
91 public static byte parseByte(String str)
92 throws NumberFormatException
94 return parseByte(str, 10);
97 public static Byte valueOf(String str, int radix)
98 throws NumberFormatException
100 return new Byte(parseByte(str, radix));
103 public static Byte valueOf(String str)
104 throws NumberFormatException
106 return valueOf(str, 10);
109 // Added in JDK 1.2
110 public int compareTo(Byte anotherByte)
112 return this.value - anotherByte.value;
115 // Added in JDK 1.2
116 public int compareTo(Object o) throws ClassCastException
118 if (o instanceof Byte)
119 return this.value - ((Byte) o).value;
120 else
121 throw new ClassCastException();
124 public boolean equals(Object obj)
126 return obj != null && (obj instanceof Byte) && ((Byte)obj).value == value;
129 // Verified that hashCode is returns plain value (see Boolean_1 test).
130 public int hashCode()
132 return value;
135 public String toString()
137 return Integer.toString((int) value);
140 public static String toString(byte value)
142 return Integer.toString((int) value);