⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 integer.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Integer.java -- object wrapper for int   Copyright (C) 1998, 1999, 2001, 2002, 2004, 2005   Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.lang;/** * Instances of class <code>Integer</code> represent primitive * <code>int</code> values. * * Additionally, this class provides various helper functions and variables * related to ints. * * @author Paul Fisher * @author John Keiser * @author Warren Levy * @author Eric Blake (ebb9@email.byu.edu) * @author Tom Tromey (tromey@redhat.com) * @since 1.0 * @status largely updated to 1.5 */public final class Integer extends Number implements Comparable{  /**   * Compatible with JDK 1.0.2+.   */  private static final long serialVersionUID = 1360826667806852920L;  /**   * The minimum value an <code>int</code> can represent is -2147483648 (or   * -2<sup>31</sup>).   */  public static final int MIN_VALUE = 0x80000000;  /**   * The maximum value an <code>int</code> can represent is 2147483647 (or   * 2<sup>31</sup> - 1).   */  public static final int MAX_VALUE = 0x7fffffff;  /**   * The primitive type <code>int</code> is represented by this   * <code>Class</code> object.   * @since 1.1   */  public static final Class TYPE = VMClassLoader.getPrimitiveClass('I');  /**   * The number of bits needed to represent an <code>int</code>.   * @since 1.5   */  public static final int SIZE = 32;  // This caches some Integer values, and is used by boxing  // conversions via valueOf().  We must cache at least -128..127;  // these constants control how much we actually cache.  private static final int MIN_CACHE = -128;  private static final int MAX_CACHE = 127;  private static Integer[] intCache = new Integer[MAX_CACHE - MIN_CACHE + 1];  /**   * The immutable value of this Integer.   *   * @serial the wrapped int   */  private final int value;  /**   * Create an <code>Integer</code> object representing the value of the   * <code>int</code> argument.   *   * @param value the value to use   */  public Integer(int value)  {    this.value = value;  }  /**   * Create an <code>Integer</code> object representing the value of the   * argument after conversion to an <code>int</code>.   *   * @param s the string to convert   * @throws NumberFormatException if the String does not contain an int   * @see #valueOf(String)   */  public Integer(String s)  {    value = parseInt(s, 10, false);  }  /**   * Converts the <code>int</code> to a <code>String</code> using   * the specified radix (base). If the radix exceeds   * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10   * is used instead. If the result is negative, the leading character is   * '-' ('\\u002D'). The remaining characters come from   * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z').   *   * @param num the <code>int</code> to convert to <code>String</code>   * @param radix the radix (base) to use in the conversion   * @return the <code>String</code> representation of the argument   */  public static String toString(int num, int radix)  {    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)      radix = 10;    // For negative numbers, print out the absolute value w/ a leading '-'.    // Use an array large enough for a binary number.    char[] buffer = new char[33];    int i = 33;    boolean isNeg = false;    if (num < 0)      {        isNeg = true;        num = -num;        // When the value is MIN_VALUE, it overflows when made positive        if (num < 0)	  {	    buffer[--i] = digits[(int) (-(num + radix) % radix)];	    num = -(num / radix);	  }      }    do      {        buffer[--i] = digits[num % radix];        num /= radix;      }    while (num > 0);    if (isNeg)      buffer[--i] = '-';    // Package constructor avoids an array copy.    return new String(buffer, i, 33 - i, true);  }  /**   * Converts the <code>int</code> to a <code>String</code> assuming it is   * unsigned in base 16.   *   * @param i the <code>int</code> to convert to <code>String</code>   * @return the <code>String</code> representation of the argument   */  public static String toHexString(int i)  {    return toUnsignedString(i, 4);  }  /**   * Converts the <code>int</code> to a <code>String</code> assuming it is   * unsigned in base 8.   *   * @param i the <code>int</code> to convert to <code>String</code>   * @return the <code>String</code> representation of the argument   */  public static String toOctalString(int i)  {    return toUnsignedString(i, 3);  }  /**   * Converts the <code>int</code> to a <code>String</code> assuming it is   * unsigned in base 2.   *   * @param i the <code>int</code> to convert to <code>String</code>   * @return the <code>String</code> representation of the argument   */  public static String toBinaryString(int i)  {    return toUnsignedString(i, 1);  }  /**   * Converts the <code>int</code> to a <code>String</code> and assumes   * a radix of 10.   *   * @param i the <code>int</code> to convert to <code>String</code>   * @return the <code>String</code> representation of the argument   * @see #toString(int, int)   */  public static String toString(int i)  {    // This is tricky: in libgcj, String.valueOf(int) is a fast native    // implementation.  In Classpath it just calls back to    // Integer.toString(int, int).    return String.valueOf(i);  }  /**   * Converts the specified <code>String</code> into an <code>int</code>   * using the specified radix (base). The string must not be <code>null</code>   * or empty. It may begin with an optional '-', which will negate the answer,   * provided that there are also valid digits. Each digit is parsed as if by   * <code>Character.digit(d, radix)</code>, and must be in the range   * <code>0</code> to <code>radix - 1</code>. Finally, the result must be   * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.   * Unlike Double.parseDouble, you may not have a leading '+'.   *   * @param str the <code>String</code> to convert   * @param radix the radix (base) to use in the conversion   * @return the <code>String</code> argument converted to <code>int</code>   * @throws NumberFormatException if <code>s</code> cannot be parsed as an   *         <code>int</code>   */  public static int parseInt(String str, int radix)  {    return parseInt(str, radix, false);  }  /**   * Converts the specified <code>String</code> into an <code>int</code>.   * This function assumes a radix of 10.   *   * @param s the <code>String</code> to convert   * @return the <code>int</code> value of <code>s</code>   * @throws NumberFormatException if <code>s</code> cannot be parsed as an   *         <code>int</code>   * @see #parseInt(String, int)   */  public static int parseInt(String s)  {    return parseInt(s, 10, false);  }  /**   * Creates a new <code>Integer</code> object using the <code>String</code>   * and specified radix (base).   *   * @param s the <code>String</code> to convert   * @param radix the radix (base) to convert with   * @return the new <code>Integer</code>   * @throws NumberFormatException if <code>s</code> cannot be parsed as an   *         <code>int</code>   * @see #parseInt(String, int)   */  public static Integer valueOf(String s, int radix)  {    return new Integer(parseInt(s, radix, false));  }  /**   * Creates a new <code>Integer</code> object using the <code>String</code>,   * assuming a radix of 10.   *   * @param s the <code>String</code> to convert   * @return the new <code>Integer</code>   * @throws NumberFormatException if <code>s</code> cannot be parsed as an   *         <code>int</code>   * @see #Integer(String)   * @see #parseInt(String)   */  public static Integer valueOf(String s)  {    return new Integer(parseInt(s, 10, false));  }  /**   * Returns an <code>Integer</code> object wrapping the value.   * In contrast to the <code>Integer</code> constructor, this method   * will cache some values.  It is used by boxing conversion.   *   * @param val the value to wrap   * @return the <code>Integer</code>   */  public static Integer valueOf(int val)  {    if (val < MIN_CACHE || val > MAX_CACHE)      return new Integer(val);    synchronized (intCache)      {	if (intCache[val - MIN_CACHE] == null)	  intCache[val - MIN_CACHE] = new Integer(val);	return intCache[val - MIN_CACHE];      }  }  /**   * Return the value of this <code>Integer</code> as a <code>byte</code>.   *   * @return the byte value   */  public byte byteValue()  {    return (byte) value;  }  /**   * Return the value of this <code>Integer</code> as a <code>short</code>.   *   * @return the short value   */  public short shortValue()  {    return (short) value;  }  /**   * Return the value of this <code>Integer</code>.   * @return the int value   */  public int intValue()  {    return value;  }  /**   * Return the value of this <code>Integer</code> as a <code>long</code>.   *   * @return the long value   */  public long longValue()  {    return value;  }  /**   * Return the value of this <code>Integer</code> as a <code>float</code>.   *   * @return the float value   */  public float floatValue()  {    return value;  }  /**   * Return the value of this <code>Integer</code> as a <code>double</code>.   *   * @return the double value   */  public double doubleValue()  {    return value;  }  /**   * Converts the <code>Integer</code> value to a <code>String</code> and   * assumes a radix of 10.   *   * @return the <code>String</code> representation   */  public String toString()  {    return String.valueOf(value);  }  /**   * Return a hashcode representing this Object. <code>Integer</code>'s hash

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -