home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / Integer.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  31.4 KB  |  872 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Integer.java    1.54 98/08/26
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Integer class wraps a value of the primitive type <code>int</code>
  19.  * in an object. An object of type <code>Integer</code> contains a
  20.  * single field whose type is <code>int</code>.
  21.  * <p>
  22.  * In addition, this class provides several methods for converting
  23.  * an <code>int</code> to a <code>String</code> and a
  24.  * <code>String</code> to an <code>int</code>, as well as other
  25.  * constants and methods useful when dealing with an
  26.  * <code>int</code>.
  27.  *
  28.  * @author  Lee Boynton
  29.  * @author  Arthur van Hoff
  30.  * @version 1.54, 08/26/98
  31.  * @since   JDK1.0
  32.  */
  33. public final class Integer extends Number implements Comparable {
  34.     /**
  35.      * The smallest value of type <code>int</code>. The constant 
  36.      * value of this field is <tt>-2147483648</tt>.
  37.      */
  38.     public static final int   MIN_VALUE = 0x80000000;
  39.  
  40.     /**
  41.      * The largest value of type <code>int</code>. The constant 
  42.      * value of this field is <tt>2147483647</tt>.
  43.      */
  44.     public static final int   MAX_VALUE = 0x7fffffff;
  45.  
  46.     /**
  47.      * The Class object representing the primitive type int.
  48.      *
  49.      * @since   JDK1.1
  50.      */
  51.     public static final Class    TYPE = Class.getPrimitiveClass("int");
  52.  
  53.     /**
  54.      * All possible chars for representing a number as a String
  55.      */
  56.     final static char[] digits = {
  57.     '0' , '1' , '2' , '3' , '4' , '5' ,
  58.     '6' , '7' , '8' , '9' , 'a' , 'b' ,
  59.     'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  60.     'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  61.     'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  62.     'u' , 'v' , 'w' , 'x' , 'y' , 'z'
  63.     };
  64.  
  65.     /**
  66.      * Array of chars to lookup the char for the digit in the tenth's
  67.      * place for a two digit, base ten number.  The char can be got by
  68.      * using the number as the index.
  69.      */
  70.     private final static char[] radixTenTenths = {
  71.     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
  72.     '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
  73.     '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
  74.     '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
  75.     '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
  76.     '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
  77.     '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
  78.     '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
  79.     '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
  80.     '9', '9', '9', '9', '9', '9', '9', '9', '9', '9'
  81.     };
  82.     /**
  83.      * Array of chars to lookup the char for the digit in the unit's
  84.      * place for a two digit, base ten number.  The char can be got by
  85.      * using the number as the index.
  86.      */
  87.     private final static char[] radixTenUnits = {
  88.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  89.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  90.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  91.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  92.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  93.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  94.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  95.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  96.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  97.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  98.     };
  99.  
  100.     /**
  101.      * Creates a string representation of the first argument in the
  102.      * radix specified by the second argument.
  103.      * <p>
  104.      * If the radix is smaller than <code>Character.MIN_RADIX</code> or
  105.      * larger than <code>Character.MAX_RADIX</code>, then the radix
  106.      * <code>10</code> is used instead.
  107.      * <p>
  108.      * If the first argument is negative, the first element of the 
  109.      * result is the ASCII minus character <code>'-'</code> 
  110.      * (<tt>'\u002d'</tt>). If the first 
  111.      * argument is not negative, no sign character appears in the result. 
  112.      * <p>
  113.      * The remaining characters of the result represent the magnitude of 
  114.      * the first argument. If the magnitude is zero, it is represented by 
  115.      * a single zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, 
  116.      * the first character of the representation of the magnitude will 
  117.      * not be the zero character. 
  118.      * The following ASCII characters are used as digits: 
  119.      * <blockquote><pre>
  120.      *   0123456789abcdefghijklmnopqrstuvwxyz
  121.      * </pre></blockquote>
  122.      * These are <tt>'\u0030'</tt> through <tt>'\u0039'</tt> and 
  123.      * <tt>'\u0061'</tt> through <tt>'\u007a'</tt>. If the 
  124.      * <tt>radix</tt> is <var>N</var>, then the first <var>N</var> of these 
  125.      * characters are used as radix-<var>N</var> digets in the order shown. 
  126.      * Thus, the digits for hexadecimal (radix 16) are 
  127.      * <tt>0123456789abcdef</tt>. If uppercase letters are desired, the 
  128.      * {@link java.lang.String#toUpperCase()} method 
  129.      * may be called on the result:
  130.      * <blockquote><pre>
  131.      * Integer.toString(n, 16).toUpperCase()
  132.      * </pre></blockquote>
  133.      *
  134.      * @param   i       an integer.
  135.      * @param   radix   the radix.
  136.      * @return  a string representation of the argument in the specified radix.
  137.      * @see     java.lang.Character#MAX_RADIX
  138.      * @see     java.lang.Character#MIN_RADIX
  139.      */
  140.     public static String toString(int i, int radix) {
  141.  
  142.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  143.         radix = 10;
  144.  
  145.     /* Use the faster version */
  146.     if (radix == 10) {
  147.         return toString(i);
  148.     }
  149.  
  150.     char buf[] = new char[33];
  151.     boolean negative = (i < 0);
  152.     int charPos = 32;
  153.  
  154.     if (!negative) {
  155.         i = -i;
  156.     }
  157.  
  158.     while (i <= -radix) {
  159.         buf[charPos--] = digits[-(i % radix)];
  160.         i = i / radix;
  161.     }
  162.     buf[charPos] = digits[-i];
  163.  
  164.     if (negative) {
  165.         buf[--charPos] = '-';
  166.     }
  167.  
  168.     return new String(buf, charPos, (33 - charPos));
  169.     }
  170.  
  171.  
  172.     /**
  173.      * Creates a string representation of the integer argument as an
  174.      * unsigned integer in base 16.
  175.      * <p>
  176.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  177.      * the argument is negative; otherwise, it is equal to the argument. 
  178.      * This value is converted to a string of ASCII digits in hexadecimal 
  179.      * (base 16) with no extra leading <code>0</code>s. If the 
  180.      * unsigned magnitude is zero, it is represented by a single zero 
  181.      * character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the first 
  182.      * character of the representation of the unsigned magnitude will 
  183.      * not be the zero character. The following characters are used as 
  184.      * hexadecimal digits:
  185.      * <blockquote><pre>
  186.      * 0123456789abcdef
  187.      * </pre></blockquote>
  188.      * These are the characters <tt>'\u0030'</tt> through <tt>'\u0039'</tt> 
  189.      * and <tt>'u\0039'</tt> through <tt>'\u0066'</tt>. If the uppercase 
  190.      * letters are desired, the {@link java.lang.String#toUpperCase()} 
  191.      * method may be called on the result:
  192.      * <blockquote><pre>
  193.      * Long.toHexString(n).toUpperCase()
  194.      * </pre></blockquote>
  195.      *
  196.      * @param   i   an integer.
  197.      * @return  the string representation of the unsigned integer value
  198.      *          represented by the argument in hexadecimal (base 16).
  199.      * @since   JDK1.0.2
  200.      */
  201.     public static String toHexString(int i) {
  202.     return toUnsignedString(i, 4);
  203.     }
  204.  
  205.     /**
  206.      * Creates a string representation of the integer argument as an
  207.      * unsigned integer in base 8.
  208.      * <p>
  209.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  210.      * the argument is negative; otherwise, it is equal to the argument. 
  211.      * This value is converted to a string of ASCII digits in octal 
  212.      * (base 8) with no extra leading <code>0</code>s. 
  213.      * <p>
  214.      * If the unsigned magnitude is zero, it is represented by a single 
  215.      * zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the 
  216.      * first character of the representation of the unsigned magnitude will 
  217.      * not be the zero character. The octal digits are:
  218.      * <blockquote><pre>
  219.      * 01234567
  220.      * </pre></blockquote>
  221.      * These are the characters <tt>'\u0030'</tt> through <tt>'\u0037'</tt>. 
  222.      *
  223.      * @param   i   an integer
  224.      * @return  the string representation of the unsigned integer value
  225.      *          represented by the argument in octal (base 8).
  226.      * @since   JDK1.0.2
  227.      */
  228.     public static String toOctalString(int i) {
  229.     return toUnsignedString(i, 3);
  230.     }
  231.  
  232.     /**
  233.      * Creates a string representation of the integer argument as an
  234.      * unsigned integer in base 2.
  235.      * <p>
  236.      * The unsigned integer value is the argument plus 2<sup>32</sup>if 
  237.      * the argument is negative; otherwise it is equal to the argument. 
  238.      * This value is converted to a string of ASCII digits in binary 
  239.      * (base 2) with no extra leading <code>0</code>s. 
  240.      * 
  241.      * If the unsigned magnitude is zero, it is represented by a single 
  242.      * zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the 
  243.      * first character of the representation of the unsigned magnitude 
  244.      * will not be the zero character. The characters <tt>'0'</tt> 
  245.      * (<tt>'\u0030'</tt>) and <tt>'1'</tt> (<tt>'\u0031'</tt>) are used 
  246.      * as binary digits.
  247.      *
  248.      * @param   i   an integer.
  249.      * @return  the string representation of the unsigned integer value
  250.      *          represented by the argument in binary (base 2).
  251.      * @since   JDK1.0.2
  252.      */
  253.     public static String toBinaryString(int i) {
  254.     return toUnsignedString(i, 1);
  255.     }
  256.  
  257.     /**
  258.      * Convert the integer to an unsigned number.
  259.      */
  260.     private static String toUnsignedString(int i, int shift) {
  261.     char[] buf = new char[32];
  262.     int charPos = 32;
  263.     int radix = 1 << shift;
  264.     int mask = radix - 1;
  265.     do {
  266.         buf[--charPos] = digits[i & mask];
  267.         i >>>= shift;
  268.     } while (i != 0);
  269.  
  270.     return new String(buf, charPos, (32 - charPos));
  271.     }
  272.  
  273.     /**
  274.      * Returns a new String object representing the specified integer. The 
  275.      * argument is converted to signed decimal representation and returned 
  276.      * as a string, exactly as if the argument and radix <tt>10</tt> were 
  277.      * given as arguments to the {@link #toString(int, int)} method.
  278.      *
  279.      * @param   i   an integer to be converted.
  280.      * @return  a string representation of the argument in base 10.
  281.      */
  282.     public static String toString(int i) {
  283.     /**
  284.      * Performance improvements -
  285.      *
  286.      * 1) Avoid a method call and radix checks by inlining the code for
  287.      *    radix = 10 in this method.
  288.      * 2) Use char arrays instead of StringBuffer and avoid calls to
  289.      *    Character.forDigit.
  290.      * 3) Do computations in positive space to avoid negation each time
  291.      *    around the loop.
  292.      * 4) Unroll loop by half and use a static array of chars to look-
  293.      *    up chars for a digit.
  294.      * The other option for 4) was to use a switch statement and assign
  295.      * the char for the current digit.  That was a little slower than 4)
  296.      * with most jits.
  297.      * Speed-up = (approximately) 4x on both Solaris and Win32.
  298.      */
  299.     char[] buf = new char[12];
  300.     boolean negative = (i < 0);
  301.     int charPos = 12;
  302.  
  303.     if (i == Integer.MIN_VALUE) {
  304.         return "-2147483648";
  305.     }
  306.  
  307.     if (negative) {
  308.         i = -i;
  309.     }
  310.  
  311.     do {
  312.         int digit = i%100;
  313.         buf[--charPos] = radixTenUnits[digit];
  314.         buf[--charPos] = radixTenTenths[digit];
  315.         i = i / 100;
  316.     } while(i != 0);
  317.  
  318.     if (buf[charPos] == '0') {
  319.         charPos++;
  320.     }
  321.     if (negative) {
  322.         buf[--charPos] = '-';
  323.     }
  324.  
  325.     return new String(buf , charPos , (12 - charPos));
  326.     }
  327.  
  328.     /**
  329.      * Parses the string argument as a signed integer in the radix 
  330.      * specified by the second argument. The characters in the string 
  331.      * must all be digits of the specified radix (as determined by 
  332.      * whether {@link java.lang.Character#digit(char, int)} returns a 
  333.      * nonnegative value), except that the first character may be an 
  334.      * ASCII minus sign <code>'-'</code> (<code>'\u002d'</code>) to 
  335.      * indicate a negative value. The resulting integer value is returned. 
  336.      * <p>
  337.      * An exception of type <tt>NumberFormatException</tt> is thrown if any 
  338.      * of the following situations occurs:
  339.      * <ul>
  340.      * <li>The first argument is <tt>null</tt> or is a string of length zero. 
  341.      * <li>The radix is either smaller than 
  342.      * {@link java.lang.Character#MIN_RADIX} or
  343.      * larger than {@link java.lang.Character#MAX_RADIX}. 
  344.      * <li>Any character of the string is not a digit of the specified radix,
  345.      * except that the first character may be a minus sign <tt>'-'</tt> 
  346.      * (<tt>'\u002d'</tt>) provided that the string is longer than length 1. 
  347.      * <li>The integer value represented by the string is not a value of type
  348.      * <tt>int</tt>. 
  349.      * </ul><p>
  350.      * Examples:
  351.      * <blockquote><pre>
  352.      * parseInt("0", 10) returns 0
  353.      * parseInt("473", 10) returns 473
  354.      * parseInt("-0", 10) returns 0
  355.      * parseInt("-FF", 16) returns -255
  356.      * parseInt("1100110", 2) returns 102
  357.      * parseInt("2147483647", 10) returns 2147483647
  358.      * parseInt("-2147483648", 10) returns -2147483648
  359.      * parseInt("2147483648", 10) throws a NumberFormatException
  360.      * parseInt("99", 8) throws a NumberFormatException
  361.      * parseInt("Kona", 10) throws a NumberFormatException
  362.      * parseInt("Kona", 27) returns 411787
  363.      * </pre></blockquote>
  364.      *
  365.      * @param      s   the <code>String</code> containing the integer.
  366.      * @param      radix   the radix to be used.
  367.      * @return     the integer represented by the string argument in the
  368.      *             specified radix.
  369.      * @exception  NumberFormatException  if the string does not contain a
  370.      *               parsable integer.
  371.  
  372.      */
  373.     public static int parseInt(String s, int radix)
  374.         throws NumberFormatException
  375.     {
  376.         if (s == null) {
  377.             throw new NumberFormatException("null");
  378.         }
  379.  
  380.     if (radix < Character.MIN_RADIX) {
  381.         throw new NumberFormatException("radix " + radix +
  382.                         " less than Character.MIN_RADIX");
  383.     }
  384.  
  385.     if (radix > Character.MAX_RADIX) {
  386.         throw new NumberFormatException("radix " + radix +
  387.                         " greater than Character.MAX_RADIX");
  388.     }
  389.  
  390.     int result = 0;
  391.     boolean negative = false;
  392.     int i = 0, max = s.length();
  393.     int limit;
  394.     int multmin;
  395.     int digit;
  396.  
  397.     if (max > 0) {
  398.         if (s.charAt(0) == '-') {
  399.         negative = true;
  400.         limit = Integer.MIN_VALUE;
  401.         i++;
  402.         } else {
  403.         limit = -Integer.MAX_VALUE;
  404.         }
  405.         multmin = limit / radix;
  406.         if (i < max) {
  407.         digit = Character.digit(s.charAt(i++),radix);
  408.         if (digit < 0) {
  409.             throw new NumberFormatException(s);
  410.         } else {
  411.             result = -digit;
  412.         }
  413.         }
  414.         while (i < max) {
  415.         // Accumulating negatively avoids surprises near MAX_VALUE
  416.         digit = Character.digit(s.charAt(i++),radix);
  417.         if (digit < 0) {
  418.             throw new NumberFormatException(s);
  419.         }
  420.         if (result < multmin) {
  421.             throw new NumberFormatException(s);
  422.         }
  423.         result *= radix;
  424.         if (result < limit + digit) {
  425.             throw new NumberFormatException(s);
  426.         }
  427.         result -= digit;
  428.         }
  429.     } else {
  430.         throw new NumberFormatException(s);
  431.     }
  432.     if (negative) {
  433.         if (i > 1) {
  434.         return result;
  435.         } else {    /* Only got "-" */
  436.         throw new NumberFormatException(s);
  437.         }
  438.     } else {
  439.         return -result;
  440.     }
  441.     }
  442.  
  443.     /**
  444.      * Parses the string argument as a signed decimal integer. The 
  445.      * characters in the string must all be decimal digits, except that 
  446.      * the first character may be an ASCII minus sign <code>'-'</code> 
  447.      * (<tt>'\u002d'</tt>) to indicate a negative value. The resulting 
  448.      * integer value is returned, exactly as if the argument and the radix 
  449.      * 10 were given as arguments to the 
  450.      * {@link #parseInt(java.lang.String, int)} method.
  451.      *
  452.      * @param      s   a string.
  453.      * @return     the integer represented by the argument in decimal.
  454.      * @exception  NumberFormatException  if the string does not contain a
  455.      *               parsable integer.
  456.      */
  457.     public static int parseInt(String s) throws NumberFormatException {
  458.     return parseInt(s,10);
  459.     }
  460.  
  461.     /**
  462.      * Returns a new Integer object initialized to the value of the
  463.      * specified String. The first argument is interpreted as representing 
  464.      * a signed integer in the radix specified by the second argument, 
  465.      * exactly as if the arguments were given to the 
  466.      * {@link #parseInt(java.lang.String, int)} method. The result is an 
  467.      * <code>Integer</code> object that represents the integer value 
  468.      * specified by the string. 
  469.      * <p>
  470.      * In other words, this method returns an <code>Integer</code> object 
  471.      * equal to the value of:
  472.      * <blockquote><pre>
  473.      * new Integer(Integer.parseInt(s, radix))  
  474.      * </pre></blockquote>
  475.      *
  476.      * @param      s   the string to be parsed.
  477.      * @param      radix the radix of the integer represented by string 
  478.      *             <tt>s</tt>
  479.      * @return     a newly constructed <code>Integer</code> initialized to the
  480.      *             value represented by the string argument in the specified
  481.      *             radix.
  482.      * @exception  NumberFormatException  if the String cannot be 
  483.      *             parsed as an <code>int</code>.
  484.      */
  485.     public static Integer valueOf(String s, int radix) throws NumberFormatException {
  486.     return new Integer(parseInt(s,radix));
  487.     }
  488.  
  489.     /**
  490.      * Returns a new Integer object initialized to the value of the
  491.      * specified String. The argument is interpreted as representing a 
  492.      * signed decimal integer, exactly as if the argument were given to 
  493.      * the {@link #parseInt(java.lang.String)} method. The result is an 
  494.      * <tt>Integer</tt> object that represents the integer value specified 
  495.      * by the string. 
  496.      * <p>
  497.      * In other words, this method returns an <tt>Integer</tt> object equal 
  498.      * to the value of:
  499.      * <blockquote><pre>
  500.      * new Integer(Integer.parseInt(s)) 
  501.      * </pre></blockquote>
  502.      *
  503.      * @param      s   the string to be parsed.
  504.      * @return     a newly constructed <code>Integer</code> initialized to the
  505.      *             value represented by the string argument.
  506.      * @exception  NumberFormatException  if the string cannot be parsed 
  507.      *             as an integer.
  508.      */
  509.     public static Integer valueOf(String s) throws NumberFormatException
  510.     {
  511.     return new Integer(parseInt(s, 10));
  512.     }
  513.  
  514.     /**
  515.      * The value of the Integer.
  516.      *
  517.      * @serial
  518.      */
  519.     private int value;
  520.  
  521.     /**
  522.      * Constructs a newly allocated <code>Integer</code> object that
  523.      * represents the primitive <code>int</code> argument.
  524.      *
  525.      * @param   value   the value to be represented by the <code>Integer</code>.
  526.      */
  527.     public Integer(int value) {
  528.     this.value = value;
  529.     }
  530.  
  531.     /**
  532.      * Constructs a newly allocated <code>Integer</code> object that 
  533.      * represents the value represented by the string. The string is 
  534.      * converted to an <tt>int</tt> in exactly the manner used by the 
  535.      * <tt>parseInt</tt> method for radix 10.
  536.      *
  537.      * @param      s   the <code>String</code> to be converted to an
  538.      *                 <code>Integer</code>.
  539.      * @exception  NumberFormatException  if the <code>String</code> does not
  540.      *               contain a parsable integer.
  541.      * @see        java.lang.Integer#parseInt(java.lang.String, int)
  542.      */
  543.     public Integer(String s) throws NumberFormatException {
  544.     this.value = parseInt(s, 10);
  545.     }
  546.  
  547.     /**
  548.      * Returns the value of this Integer as a byte.
  549.      *
  550.      * @since   JDK1.1
  551.      */
  552.     public byte byteValue() {
  553.     return (byte)value;
  554.     }
  555.  
  556.     /**
  557.      * Returns the value of this Integer as a short.
  558.      *
  559.      * @since   JDK1.1
  560.      */
  561.     public short shortValue() {
  562.     return (short)value;
  563.     }
  564.  
  565.     /**
  566.      * Returns the value of this Integer as an int.
  567.      *
  568.      * @return  the <code>int</code> value represented by this object.
  569.      */
  570.     public int intValue() {
  571.     return value;
  572.     }
  573.  
  574.     /**
  575.      * Returns the value of this Integer as a <tt>long</tt>.
  576.      *
  577.      * @return  the <code>int</code> value represented by this object that is
  578.      *          converted to type <code>long</code> and the result of the
  579.      *          conversion is returned.
  580.      */
  581.     public long longValue() {
  582.     return (long)value;
  583.     }
  584.  
  585.     /**
  586.      * Returns the value of this Integer as a <tt>float</tt>.
  587.      *
  588.      * @return  the <code>int</code> value represented by this object is
  589.      *          converted to type <code>float</code> and the result of the
  590.      *          conversion is returned.
  591.      */
  592.     public float floatValue() {
  593.     return (float)value;
  594.     }
  595.  
  596.     /**
  597.      * Returns the value of this Integer as a <tt>double</tt>.
  598.      *
  599.      * @return  the <code>int</code> value represented by this object is
  600.      *          converted to type <code>double</code> and the result of the
  601.      *          conversion is returned.
  602.      */
  603.     public double doubleValue() {
  604.     return (double)value;
  605.     }
  606.  
  607.     /**
  608.      * Returns a String object representing this Integer's value. The 
  609.      * value is converted to signed decimal representation and returned 
  610.      * as a string, exactly as if the integer value were given as an 
  611.      * argument to the {@link java.lang.Integer#toString(int)} method.
  612.      *
  613.      * @return  a string representation of the value of this object in
  614.      *          base 10.
  615.      */
  616.     public String toString() {
  617.     return String.valueOf(value);
  618.     }
  619.  
  620.     /**
  621.      * Returns a hashcode for this Integer.
  622.      *
  623.      * @return  a hash code value for this object, equal to the 
  624.      *          primitive <tt>int</tt> value represented by this 
  625.      *          <tt>Integer</tt> object. 
  626.      */
  627.     public int hashCode() {
  628.     return value;
  629.     }
  630.  
  631.     /**
  632.      * Compares this object to the specified object.
  633.      * The result is <code>true</code> if and only if the argument is not
  634.      * <code>null</code> and is an <code>Integer</code> object that contains
  635.      * the same <code>int</code> value as this object.
  636.      *
  637.      * @param   obj   the object to compare with.
  638.      * @return  <code>true</code> if the objects are the same;
  639.      *          <code>false</code> otherwise.
  640.      */
  641.     public boolean equals(Object obj) {
  642.     if ((obj != null) && (obj instanceof Integer)) {
  643.         return value == ((Integer)obj).intValue();
  644.     }
  645.     return false;
  646.     }
  647.  
  648.     /**
  649.      * Determines the integer value of the system property with the
  650.      * specified name.
  651.      * <p>
  652.      * The first argument is treated as the name of a system property. 
  653.      * System properties are accessible through the 
  654.      * {@link java.lang.System#getProperty(java.lang.String)} method. The 
  655.      * string value of this property is then interpreted as an integer 
  656.      * value and an <code>Integer</code> object representing this value is 
  657.      * returned. Details of possible numeric formats can be found with 
  658.      * the definition of <code>getProperty</code>. 
  659.      * <p>
  660.      * If there is no property with the specified name, or if the 
  661.      * property does not have the correct numeric format, then 
  662.      * <code>null</code> is returned.  In other words, this method returns 
  663.      * an <code>Integer</code> object equal to the value of:
  664.      * <blockquote><pre>
  665.      * getInteger(nm, null)
  666.      * </pre></blockquote>
  667.      *
  668.      * @param   nm   property name.
  669.      * @return  the <code>Integer</code> value of the property.
  670.      * @see     java.lang.System#getProperty(java.lang.String)
  671.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  672.      */
  673.     public static Integer getInteger(String nm) {
  674.     return getInteger(nm, null);
  675.     }
  676.  
  677.     /**
  678.      * Determines the integer value of the system property with the
  679.      * specified name.
  680.      * <p>
  681.      * The first argument is treated as the name of a system property. 
  682.      * System properties are accessible through <code>getProperty</code>, 
  683.      * a method defined by the <code>System</code> class. The 
  684.      * string value of this property is then interpreted as an integer 
  685.      * value and an <code>Integer</code> object representing this value is 
  686.      * returned. Details of possible numeric formats can be found with 
  687.      * the definition of <code>getProperty</code>. 
  688.      * <p>
  689.      * If there is no property with the specified name, or if the 
  690.      * property does not have the correct numeric format, then an 
  691.      * <code>Integer</code> object that represents the value of the 
  692.      * second argument is returned. 
  693.      * <p>
  694.      * In other words, this method returns an <code>Integer</code> object 
  695.      * equal to the value of:
  696.      * <blockquote><pre>
  697.      * getInteger(nm, new Integer(val))
  698.      * </pre></blockquote>
  699.      * but in practice it may be implemented in a manner such as: 
  700.      * <blockquote><pre>
  701.      * Integer result = getInteger(nm, null);
  702.      * return (result == null) ? new Integer(val) : result;
  703.      * </pre></blockquote>
  704.      * to avoid the unnecessary allocation of an <code>Integer</code> 
  705.      * object when the default value is not needed. 
  706.      *
  707.      * @param   nm   property name.
  708.      * @param   val   default value.
  709.      * @return  the <code>Integer</code> value of the property.
  710.      * @see     java.lang.System#getProperty(java.lang.String)
  711.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  712.      */
  713.     public static Integer getInteger(String nm, int val) {
  714.         Integer result = getInteger(nm, null);
  715.         return (result == null) ? new Integer(val) : result;
  716.     }
  717.  
  718.     /**
  719.      * Returns the integer value of the system property with the specified
  720.      * name.  The first argument is treated as the name of a system property.
  721.      * System properties are accessible through <code>getProperty</code>, 
  722.      * a method defined by the <code>System</code> class. The string value of
  723.      * this property is then interpreted as an integer value, as per the
  724.      * <code>Integer.decode</code> method, and an <code>Integer</code> object
  725.      * representing this value is returned.
  726.      * <p>
  727.      * <ul><li>If the property value begins with the two ASCII characters 
  728.      *         <code>0x</code> or the ASCII character <code>#</code>, not 
  729.      *      followed by a minus sign, then the rest of it is parsed as a 
  730.      *      hexadecimal integer exactly as for the method 
  731.      *      {@link #valueOf(java.lang.String, int)} with radix 16. 
  732.      * <li>If the property value begins with the ASCII character 
  733.      *     <code>0</code> followed by another character, it is parsed as an 
  734.      *     octal integer exactly as for the method 
  735.      *     {@link #valueOf(java.lang.String, int) with radix 8. 
  736.      * <li>Otherwise, the property value is parsed as a decimal integer 
  737.      * exactly as for the method {@link #valueOf(java.lang.String, int)} 
  738.      * with radix 10. 
  739.      * </ul><p>
  740.      * The second argument is the default value. If there is no property 
  741.      * of the specified name, or if the property does not have the 
  742.      * correct numeric format, then the second argument is returned. 
  743.      *
  744.      * @param   nm   property name.
  745.      * @param   val   default value.
  746.      * @return  the <code>Integer</code> value of the property.
  747.      * @see     java.lang.System#getProperty(java.lang.String)
  748.      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  749.      * @see java.lang.Integer#decode
  750.      */
  751.     public static Integer getInteger(String nm, Integer val) {
  752.     String v = System.getProperty(nm);
  753.     if (v != null) {
  754.         try {
  755.         return Integer.decode(v);
  756.         } catch (NumberFormatException e) {
  757.         }
  758.     }
  759.     return val;
  760.     }
  761.  
  762.     /**
  763.      * Decodes a <code>String</code> into an <code>Integer</code>.  Accepts
  764.      * decimal, hexadecimal, and octal numbers, in the following formats:
  765.      * <pre>
  766.      *     [-]    <decimal constant>
  767.      *     [-] 0x     <hex constant>
  768.      *     [-] #      <hex constant>
  769.      *     [-] 0    <octal constant>
  770.      * </pre>
  771.      *
  772.      * The constant following an (optional) negative sign and/or "radix
  773.      * specifier" is parsed as by the <code>Integer.parseInt</code> method
  774.      * with the specified radix (10, 8 or 16).  This constant must be positive
  775.      * or a NumberFormatException will result.  The result is made negative if
  776.      * first character of the specified <code>String</code> is the negative
  777.      * sign.  No whitespace characters are permitted in the
  778.      * <code>String</code>.
  779.      *
  780.      * @param     nm the <code>String</code> to decode.
  781.      * @return    the <code>Integer</code> represented by the specified string.
  782.      * @exception NumberFormatException  if the <code>String</code> does not
  783.      *            contain a parsable integer.
  784.      * @see java.lang.Integer#parseInt(String, int)
  785.      */
  786.     public static Integer decode(String nm) throws NumberFormatException {
  787.         int radix = 10;
  788.         int index = 0;
  789.         boolean negative = false;
  790.         Integer result;
  791.  
  792.         // Handle minus sign, if present
  793.         if (nm.startsWith("-")) {
  794.             negative = true;
  795.             index++;
  796.         }
  797.  
  798.         // Handle radix specifier, if present
  799.     if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  800.         index += 2;
  801.             radix = 16;
  802.     }
  803.     else if (nm.startsWith("#", index)) {
  804.         index ++;
  805.             radix = 16;
  806.     }
  807.     else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  808.         index ++;
  809.             radix = 8;
  810.     }
  811.  
  812.         if (nm.startsWith("-", index))
  813.             throw new NumberFormatException("Negative sign in wrong position");
  814.  
  815.         try {
  816.             result = Integer.valueOf(nm.substring(index), radix);
  817.             result = negative ? new Integer(-result.intValue()) : result;
  818.         } catch (NumberFormatException e) {
  819.             // If number is Integer.MIN_VALUE, we'll end up here. The next line
  820.             // handles this case, and causes any genuine format error to be
  821.             // rethrown.
  822.             String constant = negative ? new String("-" + nm.substring(index))
  823.                                        : nm.substring(index);
  824.             result = Integer.valueOf(constant, radix);
  825.         }
  826.         return result;
  827.     }
  828.  
  829.     /**
  830.      * Compares two Integers numerically.
  831.      *
  832.      * @param   anotherInteger   the <code>Integer</code> to be compared.
  833.      * @return  the value <code>0</code> if the argument Integer is equal to
  834.      *          this Integer; a value less than <code>0</code> if this Integer
  835.      *          is numerically less than the Integer argument; and a
  836.      *          value greater than <code>0</code> if this Integer is
  837.      *          numerically greater than the Integer argument
  838.      *        (signed comparison).
  839.      * @since   JDK1.2
  840.      */
  841.     public int compareTo(Integer anotherInteger) {
  842.     int thisVal = this.value;
  843.     int anotherVal = anotherInteger.value;
  844.     return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  845.     }
  846.  
  847.     /**
  848.      * Compares this Integer to another Object.  If the Object is a Integer,
  849.      * this function behaves like <code>compareTo(Integer)</code>.  Otherwise,
  850.      * it throws a <code>ClassCastException</code> (as Integers are comparable
  851.      * only to other Integers).
  852.      *
  853.      * @param   o the <code>Object</code> to be compared.
  854.      * @return  the value <code>0</code> if the argument is a Integer
  855.      *        numerically equal to this Integer; a value less than
  856.      *        <code>0</code> if the argument is a Integer numerically
  857.      *        greater than this Integer; and a value greater than
  858.      *        <code>0</code> if the argument is a Integer numerically
  859.      *        less than this Integer.
  860.      * @exception <code>ClassCastException</code> if the argument is not an
  861.      *          <code>Integer</code>.
  862.      * @see     java.lang.Comparable
  863.      * @since   JDK1.2
  864.      */
  865.     public int compareTo(Object o) {
  866.     return compareTo((Integer)o);
  867.     }
  868.  
  869.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  870.     private static final long serialVersionUID = 1360826667806852920L;
  871. }
  872.