KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > Byte


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package java.lang;
17
18 /**
19  * Wraps native <code>byte</code> as an object.
20  */

21 public final class Byte extends Number JavaDoc implements Comparable JavaDoc {
22   public static final byte MIN_VALUE = (byte) 0x80;
23   public static final byte MAX_VALUE = (byte) 0x7F;
24
25   public static Byte JavaDoc decode(String JavaDoc s) throws NumberFormatException JavaDoc {
26     return new Byte JavaDoc((byte)__decodeAndValidateLong(s, MIN_VALUE, MAX_VALUE));
27   }
28
29   public static byte parseByte(String JavaDoc s) throws NumberFormatException JavaDoc {
30     final int baseTen = 10;
31     return parseByte(s, baseTen);
32   }
33
34   public static byte parseByte(String JavaDoc s, int radix)
35       throws NumberFormatException JavaDoc {
36     return (byte)__parseAndValidateLong(s, radix, MIN_VALUE, MAX_VALUE);
37   }
38
39   public static String JavaDoc toString(byte b) {
40     return String.valueOf(b);
41   }
42
43   public static Byte JavaDoc valueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
44     return new Byte JavaDoc(Byte.parseByte(s));
45   }
46
47   public static Byte JavaDoc valueOf(String JavaDoc s, int radix) throws NumberFormatException JavaDoc {
48     return new Byte JavaDoc(Byte.parseByte(s, radix));
49   }
50
51   private final transient byte value;
52
53   public Byte(byte value) {
54     this.value = value;
55   }
56
57   public Byte(String JavaDoc s) {
58     value = parseByte(s);
59   }
60
61   public byte byteValue() {
62     return value;
63   }
64
65   public int compareTo(Byte JavaDoc b) {
66     if (value < b.value) {
67       return -1;
68     } else if (value > b.value) {
69       return 1;
70     } else {
71       return 0;
72     }
73   }
74
75   public int compareTo(Object JavaDoc o) {
76     return compareTo((Byte JavaDoc) o);
77   }
78
79   public double doubleValue() {
80     return value;
81   }
82
83   public boolean equals(Object JavaDoc o) {
84     return (o instanceof Byte JavaDoc) && (((Byte JavaDoc) o).value == value);
85   }
86
87   public float floatValue() {
88     return value;
89   }
90
91   public int hashCode() {
92     return value;
93   }
94
95   public int intValue() {
96     return value;
97   }
98
99   public long longValue() {
100     return value;
101   }
102
103   public short shortValue() {
104     return value;
105   }
106
107   public String JavaDoc toString() {
108     return toString(value);
109   }
110 }
111
Popular Tags