source: trunk/src/gcc/libjava/java/lang/Short.java@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 9.7 KB
Line 
1/* java.lang.Short
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.lang;
40
41/**
42 * Instances of class <code>Short</code> represent primitive
43 * <code>short</code> values.
44 *
45 * Additionally, this class provides various helper functions and variables
46 * related to shorts.
47 *
48 * @author Paul Fisher
49 * @author John Keiser
50 * @since JDK 1.0
51 */
52public final class Short extends Number implements Comparable
53{
54 static final long serialVersionUID = 7515723908773894738L;
55
56 /**
57 * The minimum value a <code>short</code> can represent is -32768.
58 */
59 public static final short MIN_VALUE = -32768;
60
61 /**
62 * The minimum value a <code>short</code> can represent is 32767.
63 */
64 public static final short MAX_VALUE = 32767;
65
66 /**
67 * The primitive type <code>short</code> is represented by this
68 * <code>Class</code> object.
69 */
70 public static final Class TYPE = VMClassLoader.getPrimitiveClass('S');
71
72 /**
73 * The immutable value of this Short.
74 */
75 private final short value;
76
77 /**
78 * Create a <code>Short</code> object representing the value of the
79 * <code>short</code> argument.
80 *
81 * @param value the value to use
82 */
83 public Short(short value)
84 {
85 this.value = value;
86 }
87
88 /**
89 * Create a <code>Short</code> object representing the value of the
90 * argument after conversion to a <code>short</code>.
91 *
92 * @param s the string to convert.
93 */
94 public Short(String s) throws NumberFormatException
95 {
96 value = parseShort(s, 10);
97 }
98
99 /**
100 * Return a hashcode representing this Object.
101 *
102 * <code>Short</code>'s hash code is calculated by simply returning its
103 * value.
104 *
105 * @return this Object's hash code.
106 */
107 public int hashCode()
108 {
109 return value;
110 }
111
112 /**
113 * If the <code>Object</code> is not <code>null</code>, is an
114 * <code>instanceof</code> <code>Short</code>, and represents
115 * the same primitive <code>short</code> value return
116 * <code>true</code>. Otherwise <code>false</code> is returned.
117 */
118 public boolean equals(Object obj)
119 {
120 return obj instanceof Short && ((Short)obj).value == value;
121 }
122
123 /**
124 * Converts the <code>short</code> to a <code>String</code> and assumes
125 * a radix of 10.
126 * @param i the <code>short</code> to convert to <code>String</code>
127 * @return the <code>String</code> representation of the argument.
128 */
129 public static String toString(short i)
130 {
131 return Integer.toString((int) i);
132 }
133
134 /**
135 * Converts the <code>Short</code> value to a <code>String</code> and
136 * assumes a radix of 10.
137 * @return the <code>String</code> representation of this <code>Short</code>.
138 */
139 public String toString()
140 {
141 return Integer.toString ((int) value);
142 }
143
144 /**
145 * Creates a new <code>Short</code> object using the <code>String</code>,
146 * assuming a radix of 10.
147 * @param s the <code>String</code> to convert.
148 * @return the new <code>Short</code>.
149 * @see #Short(java.lang.String)
150 * @see #parseShort(java.lang.String)
151 * @exception NumberFormatException thrown if the <code>String</code>
152 * cannot be parsed as a <code>short</code>.
153 */
154 public static Short valueOf(String s) throws NumberFormatException
155 {
156 return new Short(parseShort(s));
157 }
158
159 /**
160 * Creates a new <code>Short</code> object using the <code>String</code>
161 * and specified radix (base).
162 * @param s the <code>String</code> to convert.
163 * @param radix the radix (base) to convert with.
164 * @return the new <code>Short</code>.
165 * @see #parseShort(java.lang.String,int)
166 * @exception NumberFormatException thrown if the <code>String</code>
167 * cannot be parsed as a <code>short</code>.
168 */
169 public static Short valueOf(String s, int radix)
170 throws NumberFormatException
171 {
172 return new Short(parseShort(s, radix));
173 }
174
175 /**
176 * Converts the specified <code>String</code> into a <code>short</code>.
177 * This function assumes a radix of 10.
178 *
179 * @param s the <code>String</code> to convert
180 * @return the <code>short</code> value of the <code>String</code>
181 * argument.
182 * @exception NumberFormatException thrown if the <code>String</code>
183 * cannot be parsed as a <code>short</code>.
184 */
185 public static short parseShort(String s) throws NumberFormatException
186 {
187 return parseShort(s, 10);
188 }
189
190 /**
191 * Converts the specified <code>String</code> into a <code>short</code>
192 * using the specified radix (base).
193 *
194 * @param s the <code>String</code> to convert
195 * @param radix the radix (base) to use in the conversion
196 * @return the <code>String</code> argument converted to </code>short</code>.
197 * @exception NumberFormatException thrown if the <code>String</code>
198 * cannot be parsed as a <code>short</code>.
199 */
200 public static short parseShort(String s, int radix)
201 throws NumberFormatException