9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Native;
29 import java.util.Properties;
30
31 /**
32 * The {@code Integer} class wraps a value of the primitive type
33 * {@code int} in an object. An object of type {@code Integer}
34 * contains a single field whose type is {@code int}.
35 *
36 * <p>In addition, this class provides several methods for converting
37 * an {@code int} to a {@code String} and a {@code String} to an
38 * {@code int}, as well as other constants and methods useful when
39 * dealing with an {@code int}.
40 *
41 * <p>Implementation note: The implementations of the "bit twiddling"
42 * methods (such as {@link #highestOneBit(int) highestOneBit} and
43 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
44 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
45 * Delight</i>, (Addison Wesley, 2002).
46 *
47 * @author Lee Boynton
48 * @author Arthur van Hoff
49 * @author Josh Bloch
291 * otherwise, the first character of the representation of the
292 * unsigned magnitude will not be the zero character. The
293 * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
294 * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
295 *
296 * @param i an integer to be converted to a string.
297 * @return the string representation of the unsigned integer value
298 * represented by the argument in binary (base 2).
299 * @see #parseUnsignedInt(String, int)
300 * @see #toUnsignedString(int, int)
301 * @since JDK1.0.2
302 */
303 public static String toBinaryString(int i) {
304 return toUnsignedString0(i, 1);
305 }
306
307 /**
308 * Convert the integer to an unsigned number.
309 */
310 private static String toUnsignedString0(int i, int shift) {
311 char[] buf = new char[32];
312 int charPos = 32;
313 int radix = 1 << shift;
314 int mask = radix - 1;
315 do {
316 buf[--charPos] = digits[i & mask];
317 i >>>= shift;
318 } while (i != 0);
319
320 return new String(buf, charPos, (32 - charPos));
321 }
322
323
324 final static char [] DigitTens = {
325 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
326 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
327 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
328 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
329 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
330 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
331 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
332 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
333 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
334 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
335 } ;
336
337 final static char [] DigitOnes = {
338 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
339 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
340 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
858 * Returns the value of this {@code Integer} as a {@code short}
859 * after a narrowing primitive conversion.
860 * @jls 5.1.3 Narrowing Primitive Conversions
861 */
862 public short shortValue() {
863 return (short)value;
864 }
865
866 /**
867 * Returns the value of this {@code Integer} as an
868 * {@code int}.
869 */
870 public int intValue() {
871 return value;
872 }
873
874 /**
875 * Returns the value of this {@code Integer} as a {@code long}
876 * after a widening primitive conversion.
877 * @jls 5.1.2 Widening Primitive Conversions
878 */
879 public long longValue() {
880 return (long)value;
881 }
882
883 /**
884 * Returns the value of this {@code Integer} as a {@code float}
885 * after a widening primitive conversion.
886 * @jls 5.1.2 Widening Primitive Conversions
887 */
888 public float floatValue() {
889 return (float)value;
890 }
891
892 /**
893 * Returns the value of this {@code Integer} as a {@code double}
894 * after a widening primitive conversion.
895 * @jls 5.1.2 Widening Primitive Conversions
896 */
897 public double doubleValue() {
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.lang.annotation.Native;
29
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
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 i, int shift) {
310 // assert shift > 0 && shift <=5 : "Illegal shift value";
311 int mag = Integer.SIZE - Long.numberOfLeadingZeros(i);
312 int chars = Math.max(((mag + (shift - 1)) / shift), 1);
313 char[] buf = new char[chars];
314 int charPos = chars;
315 int radix = 1 << shift;
316 int mask = radix - 1;
317 do {
318 buf[--charPos] = digits[i & mask];
319 i >>>= shift;
320 } while (i != 0);
321
322 // Use special constructor which takes over "buf".
323 return new String(buf, true);
324 }
325
326
327 final static char [] DigitTens = {
328 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
329 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
330 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
331 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
332 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
333 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
334 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
335 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
336 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
337 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
338 } ;
339
340 final static char [] DigitOnes = {
341 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
342 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
343 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
861 * Returns the value of this {@code Integer} as a {@code short}
862 * after a narrowing primitive conversion.
863 * @jls 5.1.3 Narrowing Primitive Conversions
864 */
865 public short shortValue() {
866 return (short)value;
867 }
868
869 /**
870 * Returns the value of this {@code Integer} as an
871 * {@code int}.
872 */
873 public int intValue() {
874 return value;
875 }
876
877 /**
878 * Returns the value of this {@code Integer} as a {@code long}
879 * after a widening primitive conversion.
880 * @jls 5.1.2 Widening Primitive Conversions
881 * @see Integer#toUnsignedLong(int)
882 */
883 public long longValue() {
884 return (long)value;
885 }
886
887 /**
888 * Returns the value of this {@code Integer} as a {@code float}
889 * after a widening primitive conversion.
890 * @jls 5.1.2 Widening Primitive Conversions
891 */
892 public float floatValue() {
893 return (float)value;
894 }
895
896 /**
897 * Returns the value of this {@code Integer} as a {@code double}
898 * after a widening primitive conversion.
899 * @jls 5.1.2 Widening Primitive Conversions
900 */
901 public double doubleValue() {
|