integer.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 562 行 · 第 1/2 页
JAVA
562 行
}
/**
* 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
* code is simply its value.
*
* @return this Object's hash code
*/
public int hashCode() {
return value;
}
/**
* Returns <code>true</code> if <code>obj</code> is an instance of
* <code>Integer</code> and represents the same int value.
*
* @param obj the object to compare
* @return whether these Objects are semantically equal
*/
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 null if the
* property is not found or cannot be decoded
* @throws SecurityException if accessing the system property is forbidden
* @see System#getProperty(String)
* @see #decode(String)
*/
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
* @return the value of the system property, or the default
* @throws SecurityException if accessing the system property is forbidden
* @see System#getProperty(String)
* @see #decode(String)
*/
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
* @return the value of the system property, or the default
* @throws SecurityException if accessing the system property is forbidden
* @see System#getProperty(String)
* @see #decode(String)
*/
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;
}
}
/**
* Convert the specified <code>String</code> into an <code>Integer</code>.
* The <code>String</code> may represent decimal, hexadecimal, or
* octal numbers.
*
* <p>The extended BNF grammar is as follows:<br>
* <pre>
* <em>DecodableString</em>:
* ( [ <code>-</code> ] <em>DecimalNumber</em> )
* | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
* | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
* | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
* <em>DecimalNumber</em>:
* <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
* <em>DecimalDigit</em>:
* <em>Character.digit(d, 10) has value 0 to 9</em>
* <em>OctalDigit</em>:
* <em>Character.digit(d, 8) has value 0 to 7</em>
* <em>DecimalDigit</em>:
* <em>Character.digit(d, 16) has value 0 to 15</em>
* </pre>
* Finally, the value must be in the range <code>MIN_VALUE</code> to
* <code>MAX_VALUE</code>, or an exception is thrown.
*
* @param s the <code>String</code> to interpret
* @return the value of the String as an <code>Integer</code>
* @throws NumberFormatException if <code>s</code> cannot be parsed as a
* <code>int</code>
* @throws NullPointerException if <code>s</code> is null
* @since 1.2
*/
public static Integer decode(String str) {
return new Integer(parseInt(str, 10, true));
}
/**
* Compare two Integers numerically by comparing their <code>int</code>
* values. The result is positive if the first is greater, negative if the
* second is greater, and 0 if the two are equal.
*
* @param i the Integer to compare
* @return the comparison
* @since 1.2
*/
public int compareTo(Integer i) {
if (value == i.value)
return 0;
// Returns just -1 or 1 on inequality; doing math might overflow.
return value > i.value ? 1 : -1;
}
/**
* Behaves like <code>compareTo(Integer)</code> unless the Object
* is not an <code>Integer</code>.
*
* @param o the object to compare
* @return the comparison
* @throws ClassCastException if the argument is not an <code>Integer</code>
* @see #compareTo(Integer)
* @see Comparable
* @since 1.2
*/
public int compareTo(Object o) {
return compareTo((Integer)o);
}
/**
* Helper for converting unsigned numbers to String.
*
* @param num the number
* @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex)
*/
// Package visible for use by Long.
static String toUnsignedString(int num, int exp) {
// Use an array large enough for a binary number.
int mask = (1 << exp) - 1;
char[] buffer = new char[32];
int i = 32;
do {
buffer[--i] = digits[num & mask];
num >>>= exp;
} while (num != 0);
// Package constructor avoids an array copy.
return new String(buffer, i, 32 - i, true);
}
/**
* Helper for parsing ints, used by Integer, Short, and Byte.
*
* @param str the string to parse
* @param radix the radix to use, must be 10 if decode is true
* @param decode if called from decode
* @return the parsed int value
* @throws NumberFormatException if there is an error
* @throws NullPointerException if decode is true and str if null
* @see #parseInt(String, int)
* @see #decode(String)
* @see Byte#parseInt(String, int)
* @see Short#parseInt(String, int)
*/
static int parseInt(String str, int radix, boolean decode) {
if (!decode && str == null)
throw new NumberFormatException();
int index = 0;
int len = str.length();
boolean isNeg = false;
if (len == 0)
throw new NumberFormatException();
int ch = str.charAt(index);
if (ch == '-') {
if (len == 1)
throw new NumberFormatException();
isNeg = true;
ch = str.charAt(++index);
}
if (decode) {
if (ch == '0') {
if (++index == len)
return 0;
if ((str.charAt(index) & ~('x' ^ 'X')) == 'X') {
radix = 16;
index++;
} else
radix = 8;
} else if (ch == '#') {
radix = 16;
index++;
}
}
if (index == len)
throw new NumberFormatException();
int max = MAX_VALUE / radix;
// We can't directly write `max = (MAX_VALUE + 1) / radix'.
// So instead we fake it.
if (isNeg && MAX_VALUE % radix == radix - 1)
++max;
int val = 0;
while (index < len) {
if (val < 0 || val > max)
throw new NumberFormatException();
ch = Character.digit(str.charAt(index++), radix);
val = val * radix + ch;
if (ch < 0 || (val < 0 && (!isNeg || val != MIN_VALUE)))
throw new NumberFormatException();
}
return isNeg ? -val : val;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?