integer.java
来自「gcc3.2.1源代码」· Java 代码 · 共 593 行 · 第 1/2 页
JAVA
593 行
/* java.lang.Integer Copyright (C) 1998, 1999, 2001 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., 59 Temple Place, Suite 330, Boston, MA02111-1307 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 * @since JDK 1.0 */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. */ public static final int MIN_VALUE = 0x80000000; /** * The maximum value an <code>int</code> can represent is 2147483647. */ public static final int MAX_VALUE = 0x7fffffff; /** * The primitive type <code>int</code> is represented by this * <code>Class</code> object. */ public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I'); /** * The immutable value of this Integer. */ 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. */ public Integer(String s) throws NumberFormatException { value = parseInt(s, 10); } /** * Return a hashcode representing this Object. * * <code>Integer</code>'s hash code is calculated by simply returning its * value. * * @return this Object's hash code. */ public int hashCode() { return value; } /** * If the <code>Object</code> is not <code>null</code>, is an * <code>instanceof</code> <code>Integer</code>, and represents * the same primitive <code>int</code> value return * <code>true</code>. Otherwise <code>false</code> is returned. */ public boolean equals(Object obj) { return obj instanceof Integer && value == ((Integer)obj).value; } /** * Get the specified system property as an <code>Integer</code>. * * The <code>decode()</code> method will be used to interpret the value of * the property. * @param nm the name of the system property * @return the system property as an <code>Integer</code>, or * <code>null</code> if the property is not found or cannot be * decoded as an <code>Integer</code>. * @see java.lang.System#getProperty(java.lang.String) * @see #decode(int) */ public static Integer getInteger(String nm) { return getInteger(nm, null); } /** * Get the specified system property as an <code>Integer</code>, or use a * default <code>int</code> value if the property is not found or is not * decodable. * * The <code>decode()</code> method will be used to interpret the value of * the property. * * @param nm the name of the system property * @param val the default value to use if the property is not found or not * a number. * @return the system property as an <code>Integer</code>, or the default * value if the property is not found or cannot be decoded as an * <code>Integer</code>. * @see java.lang.System#getProperty(java.lang.String) * @see #decode(int) * @see #getInteger(java.lang.String,java.lang.Integer) */ public static Integer getInteger(String nm, int val) { Integer result = getInteger(nm, null); return (result == null) ? new Integer(val) : result; } /** * Get the specified system property as an <code>Integer</code>, or use a * default <code>Integer</code> value if the property is not found or is * not decodable. * * The <code>decode()</code> method will be used to interpret the value of * the property. * * @param nm the name of the system property * @param val the default value to use if the property is not found or not * a number. * @return the system property as an <code>Integer</code>, or the default * value if the property is not found or cannot be decoded as an * <code>Integer</code>. * @see java.lang.System#getProperty(java.lang.String) * @see #decode(int) * @see #getInteger(java.lang.String,int) */ public static Integer getInteger(String nm, Integer def) { if (nm == null || "".equals(nm)) return def; nm = System.getProperty(nm); if (nm == null) return def; try { return decode(nm); } catch (NumberFormatException e) { return def; } } private static String toUnsignedString(int num, int exp) { // Use an array large enough for a binary number. int radix = 1 << exp; int mask = radix - 1; char[] buffer = new char[32]; int i = 32; do { buffer[--i] = Character.forDigit(num & mask, radix); num = num >>> exp; } while (num != 0); return String.valueOf(buffer, i, 32-i); } /** * 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. */ 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 <code>Integer</code> value to a <code>String</code> and * assumes a radix of 10. * @return the <code>String</code> representation of this <code>Integer</code>. */ public String toString() { return toString (value); } /** * Converts the <code>int</code> to a <code>String</code> using * the specified radix (base). * @param i 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; if (num < 0) { isNeg = true; num = -(num); // When the value is MIN_VALUE, it overflows when made positive if (num < 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?