30 /**
31 * The {@code Integer} class wraps a value of the primitive type
32 * {@code int} in an object. An object of type {@code Integer}
33 * contains a single field whose type is {@code int}.
34 *
35 * <p>In addition, this class provides several methods for converting
36 * an {@code int} to a {@code String} and a {@code String} to an
37 * {@code int}, as well as other constants and methods useful when
38 * dealing with an {@code int}.
39 *
40 * <p>Implementation note: The implementations of the "bit twiddling"
41 * methods (such as {@link #highestOneBit(int) highestOneBit} and
42 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
43 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
44 * Delight</i>, (Addison Wesley, 2002).
45 *
46 * @author Lee Boynton
47 * @author Arthur van Hoff
48 * @author Josh Bloch
49 * @author Joseph D. Darcy
50 * @since JDK1.0
51 */
52 public final class Integer extends Number implements Comparable<Integer> {
53 /**
54 * A constant holding the minimum value an {@code int} can
55 * have, -2<sup>31</sup>.
56 */
57 @Native public static final int MIN_VALUE = 0x80000000;
58
59 /**
60 * A constant holding the maximum value an {@code int} can
61 * have, 2<sup>31</sup>-1.
62 */
63 @Native public static final int MAX_VALUE = 0x7fffffff;
64
65 /**
66 * The {@code Class} instance representing the primitive type
67 * {@code int}.
68 *
69 * @since JDK1.1
70 */
71 @SuppressWarnings("unchecked")
72 public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
73
74 /**
75 * All possible chars for representing a number as a String
76 */
77 final static char[] digits = {
78 '0' , '1' , '2' , '3' , '4' , '5' ,
79 '6' , '7' , '8' , '9' , 'a' , 'b' ,
80 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
81 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
82 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
83 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
84 };
85
86 /**
87 * Returns a string representation of the first argument in the
88 * radix specified by the second argument.
89 *
210 *
211 * <blockquote>
212 * {@code 0123456789abcdef}
213 * </blockquote>
214 *
215 * These are the characters {@code '\u005Cu0030'} through
216 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
217 * {@code '\u005Cu0066'}. If uppercase letters are
218 * desired, the {@link java.lang.String#toUpperCase()} method may
219 * be called on the result:
220 *
221 * <blockquote>
222 * {@code Integer.toHexString(n).toUpperCase()}
223 * </blockquote>
224 *
225 * @param i an integer to be converted to a string.
226 * @return the string representation of the unsigned integer value
227 * represented by the argument in hexadecimal (base 16).
228 * @see #parseUnsignedInt(String, int)
229 * @see #toUnsignedString(int, int)
230 * @since JDK1.0.2
231 */
232 public static String toHexString(int i) {
233 return toUnsignedString0(i, 4);
234 }
235
236 /**
237 * Returns a string representation of the integer argument as an
238 * unsigned integer in base 8.
239 *
240 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
241 * if the argument is negative; otherwise, it is equal to the
242 * argument. This value is converted to a string of ASCII digits
243 * in octal (base 8) with no extra leading {@code 0}s.
244 *
245 * <p>The value of the argument can be recovered from the returned
246 * string {@code s} by calling {@link
247 * Integer#parseUnsignedInt(String, int)
248 * Integer.parseUnsignedInt(s, 8)}.
249 *
250 * <p>If the unsigned magnitude is zero, it is represented by a
251 * single zero character {@code '0'} ({@code '\u005Cu0030'});
252 * otherwise, the first character of the representation of the
253 * unsigned magnitude will not be the zero character. The
254 * following characters are used as octal digits:
255 *
256 * <blockquote>
257 * {@code 01234567}
258 * </blockquote>
259 *
260 * These are the characters {@code '\u005Cu0030'} through
261 * {@code '\u005Cu0037'}.
262 *
263 * @param i an integer to be converted to a string.
264 * @return the string representation of the unsigned integer value
265 * represented by the argument in octal (base 8).
266 * @see #parseUnsignedInt(String, int)
267 * @see #toUnsignedString(int, int)
268 * @since JDK1.0.2
269 */
270 public static String toOctalString(int i) {
271 return toUnsignedString0(i, 3);
272 }
273
274 /**
275 * Returns a string representation of the integer argument as an
276 * unsigned integer in base 2.
277 *
278 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
279 * if the argument is negative; otherwise it is equal to the
280 * argument. This value is converted to a string of ASCII digits
281 * in binary (base 2) with no extra leading {@code 0}s.
282 *
283 * <p>The value of the argument can be recovered from the returned
284 * string {@code s} by calling {@link
285 * Integer#parseUnsignedInt(String, int)
286 * Integer.parseUnsignedInt(s, 2)}.
287 *
288 * <p>If the unsigned magnitude is zero, it is represented by a
289 * single zero character {@code '0'} ({@code '\u005Cu0030'});
290 * otherwise, the first character of the representation of the
291 * unsigned magnitude will not be the zero character. The
292 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
293 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
294 *
295 * @param i an integer to be converted to a string.
296 * @return the string representation of the unsigned integer value
297 * represented by the argument in binary (base 2).
298 * @see #parseUnsignedInt(String, int)
299 * @see #toUnsignedString(int, int)
300 * @since JDK1.0.2
301 */
302 public static String toBinaryString(int i) {
303 return toUnsignedString0(i, 1);
304 }
305
306 /**
307 * Convert the integer to an unsigned number.
308 */
309 private static String toUnsignedString0(int val, int shift) {
310 // assert shift > 0 && shift <=5 : "Illegal shift value";
311 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
312 int chars = Math.max(((mag + (shift - 1)) / shift), 1);
313 char[] buf = new char[chars];
314
315 formatUnsignedInt(val, shift, buf, 0, chars);
316
317 // Use special constructor which takes over "buf".
318 return new String(buf, true);
319 }
320
|
30 /**
31 * The {@code Integer} class wraps a value of the primitive type
32 * {@code int} in an object. An object of type {@code Integer}
33 * contains a single field whose type is {@code int}.
34 *
35 * <p>In addition, this class provides several methods for converting
36 * an {@code int} to a {@code String} and a {@code String} to an
37 * {@code int}, as well as other constants and methods useful when
38 * dealing with an {@code int}.
39 *
40 * <p>Implementation note: The implementations of the "bit twiddling"
41 * methods (such as {@link #highestOneBit(int) highestOneBit} and
42 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
43 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
44 * Delight</i>, (Addison Wesley, 2002).
45 *
46 * @author Lee Boynton
47 * @author Arthur van Hoff
48 * @author Josh Bloch
49 * @author Joseph D. Darcy
50 * @since 1.0
51 */
52 public final class Integer extends Number implements Comparable<Integer> {
53 /**
54 * A constant holding the minimum value an {@code int} can
55 * have, -2<sup>31</sup>.
56 */
57 @Native public static final int MIN_VALUE = 0x80000000;
58
59 /**
60 * A constant holding the maximum value an {@code int} can
61 * have, 2<sup>31</sup>-1.
62 */
63 @Native public static final int MAX_VALUE = 0x7fffffff;
64
65 /**
66 * The {@code Class} instance representing the primitive type
67 * {@code int}.
68 *
69 * @since 1.1
70 */
71 @SuppressWarnings("unchecked")
72 public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
73
74 /**
75 * All possible chars for representing a number as a String
76 */
77 final static char[] digits = {
78 '0' , '1' , '2' , '3' , '4' , '5' ,
79 '6' , '7' , '8' , '9' , 'a' , 'b' ,
80 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
81 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
82 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
83 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
84 };
85
86 /**
87 * Returns a string representation of the first argument in the
88 * radix specified by the second argument.
89 *
210 *
211 * <blockquote>
212 * {@code 0123456789abcdef}
213 * </blockquote>
214 *
215 * These are the characters {@code '\u005Cu0030'} through
216 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
217 * {@code '\u005Cu0066'}. If uppercase letters are
218 * desired, the {@link java.lang.String#toUpperCase()} method may
219 * be called on the result:
220 *
221 * <blockquote>
222 * {@code Integer.toHexString(n).toUpperCase()}
223 * </blockquote>
224 *
225 * @param i an integer to be converted to a string.
226 * @return the string representation of the unsigned integer value
227 * represented by the argument in hexadecimal (base 16).
228 * @see #parseUnsignedInt(String, int)
229 * @see #toUnsignedString(int, int)
230 * @since 1.0.2
231 */
232 public static String toHexString(int i) {
233 return toUnsignedString0(i, 4);
234 }
235
236 /**
237 * Returns a string representation of the integer argument as an
238 * unsigned integer in base 8.
239 *
240 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
241 * if the argument is negative; otherwise, it is equal to the
242 * argument. This value is converted to a string of ASCII digits
243 * in octal (base 8) with no extra leading {@code 0}s.
244 *
245 * <p>The value of the argument can be recovered from the returned
246 * string {@code s} by calling {@link
247 * Integer#parseUnsignedInt(String, int)
248 * Integer.parseUnsignedInt(s, 8)}.
249 *
250 * <p>If the unsigned magnitude is zero, it is represented by a
251 * single zero character {@code '0'} ({@code '\u005Cu0030'});
252 * otherwise, the first character of the representation of the
253 * unsigned magnitude will not be the zero character. The
254 * following characters are used as octal digits:
255 *
256 * <blockquote>
257 * {@code 01234567}
258 * </blockquote>
259 *
260 * These are the characters {@code '\u005Cu0030'} through
261 * {@code '\u005Cu0037'}.
262 *
263 * @param i an integer to be converted to a string.
264 * @return the string representation of the unsigned integer value
265 * represented by the argument in octal (base 8).
266 * @see #parseUnsignedInt(String, int)
267 * @see #toUnsignedString(int, int)
268 * @since 1.0.2
269 */
270 public static String toOctalString(int i) {
271 return toUnsignedString0(i, 3);
272 }
273
274 /**
275 * Returns a string representation of the integer argument as an
276 * unsigned integer in base 2.
277 *
278 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
279 * if the argument is negative; otherwise it is equal to the
280 * argument. This value is converted to a string of ASCII digits
281 * in binary (base 2) with no extra leading {@code 0}s.
282 *
283 * <p>The value of the argument can be recovered from the returned
284 * string {@code s} by calling {@link
285 * Integer#parseUnsignedInt(String, int)
286 * Integer.parseUnsignedInt(s, 2)}.
287 *
288 * <p>If the unsigned magnitude is zero, it is represented by a
289 * single zero character {@code '0'} ({@code '\u005Cu0030'});
290 * otherwise, the first character of the representation of the
291 * unsigned magnitude will not be the zero character. The
292 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
293 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
294 *
295 * @param i an integer to be converted to a string.
296 * @return the string representation of the unsigned integer value
297 * represented by the argument in binary (base 2).
298 * @see #parseUnsignedInt(String, int)
299 * @see #toUnsignedString(int, int)
300 * @since 1.0.2
301 */
302 public static String toBinaryString(int i) {
303 return toUnsignedString0(i, 1);
304 }
305
306 /**
307 * Convert the integer to an unsigned number.
308 */
309 private static String toUnsignedString0(int val, int shift) {
310 // assert shift > 0 && shift <=5 : "Illegal shift value";
311 int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
312 int chars = Math.max(((mag + (shift - 1)) / shift), 1);
313 char[] buf = new char[chars];
314
315 formatUnsignedInt(val, shift, buf, 0, chars);
316
317 // Use special constructor which takes over "buf".
318 return new String(buf, true);
319 }
320
|