【Java】关于Integer类的integer.tostring作用方法()

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&java中math类方法简介
&&java.lang.Math类中包含基本的数字操作,如指数、对数、平方根和三角函数。
&&java.math是一个包,提供用于执行任意精度整数(BigInteger)算法和任意精度小数(BigDecimal)算法的类。
&&java.lang.Math类中包含E和PI两个静态常量,以及进行科学计算的类(static)方法,可以直接通过类名调用。
&&public static final Double E =
2.0452354&
&&public static final Double PI =
&&public static long abs(double
x):传回 x 的绝对值。X也可int long float
&&public static long sin(double
x): 传回x径度的正弦函数值&&
&&public static long cos(double
x):传回x径度的余弦函数值& &
&&public static long tan(double
x): 传回x径度的正切函数值&
&&public static long asin(double
x):传回x值的反正弦函数值。
&&public static long acos(double
x):传回x值的反余弦函数值。
&&public static long atan(double
x):传回x值的反正切函数值。&
&&public static long atan2(double
x, double y):传回极坐标(polar)的θ值&
&&public static long floor(double
x):传回不大于x的最大整数值&
&&public static long ceil(double
x):传回不小于x的最小整数值。&
&&public static long exp(double
x):传回相当于ex值&
&&public static long log(double
x):传回x的自然对数函数值&
&&public static long max(double
x,double y):传回x、y较大数&
&&public static long min(double
x,double y):传回x、y较小数&
&&public static long pow(double
x,double y):传回x的y次幂值&
&&public static long sqrt(double
x): 传回x开平方值&
&&public static long rint(double
x):传回最接近x的整数值&
&&public static long round(double
x):传回x的四舍五入值&
&&public static long
toDegrees(double
angrad):传回将angrad径度转换成角度&
&&public static long
toRadians(double angdeg): 传回将angdeg角度转换成径度
&&public static long
random():传回随机数值,产生一个0-1之间的随机数(不包括0和1)
NumberFormat类:(public abstract class NumberFormat extends
&&用java.text.NumberFormat类对输出的数字结果进行必要的格式化。
&&使用该类如下方法类实例化一个NumberFormat对象:
& &public static final
NumberFormat getInstance()
&&然后用该对象调用如下方法可以格式化数字number(返回字符串对象):
& &public final String
format(double number)
&&NumberFormat类有如下常用方法:
&&public void
setMaximumFractionDigits(int newValue)//设置数的小数部分所允许的最大位数。
&&public void
setMaximumIntegerDigits(int newValue)//设置数的整数部分所允许的最大位数。
&&public void
setMinimumFractionDigits(int newValue)//设置数的小数部分所允许的最小位数。
&&public void
setMinimumIntegerDigits(int newValue)//设置数的整数部分所允许的最小位数。
&&(更多方法及用法看JDK6API参考。)
BigInteger类、BigDecimal类:
&&java.math包中的BigInteger类和BigDecimal类分别提供任意精度的整数和小数运算。
&&两个类设计思想一样,这里只对BigInterger类稍做介绍,细节可以查看JDK6API参考。
&&构造方法如下:
& &BigInteger(String
val)&&//将 BigInteger
的十进制字符串表示形式转换为 BigInteger。
&还有很多考虑周全的构造函数,建议要准备写特殊需求的BigInteger前先认真翻下JDK。
&&常用方法:
&abs()&&//返回其值是此BigInteger的绝对值的BigInteger。
& &add(BigInteger
val)&&//返回其值为(this+val)的BigInteger。
& &subtract(BigInteger
val)&&//返回其值为(this-val)的BigInteger。
& &multiply(BigInteger
返回其值为(this*val)的BigInteger。
& &divide(BigInteger
val)&&//返回其值为(this/val)的BigInteger。
& &remainder(BigInteger
val)&&//返回其值为(this%val)的BigInteger。
& &compareTo(BigInteger
val)&&//将此BigInteger与指定的BigInteger进行比较。返回值1、0、-1分别表示大于、等于、小于
& &pow(int
exponent)&&//返回当前大数的exponent次幂。
&toString()&&//返回此BigInteger的十进制字符串表示形式。
& &toString(int
radix)&&//返回此BigInteger的给定基数(radix进制)的字符串表示形式。
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。您的位置: &
& Java源码学习之Integer类(一)
断断续续地,看完了Integer类,基本上了解了主要函数的原理之类的,下面是完整的源码和我的粗略翻译、注释以及一些吐槽。。有错请纠错QAQ。源码加注释之类的大概有1600多行,之后会挑几个重点难点的函数单独开博文讲,这里就当是个纪念~~学习的第一个Java类~
以下源码来自jdk的src.zip。
package java.
import java.lang.annotation.N
* The {@code Integer} class wraps a value of the primitive type
* {@code int} in an object. An object of type {@code Integer}
* contains a single field whose type is {@code int}.
* &p&In addition, this class provides several methods for converting
* an {@code int} to a {@code String} and a {@code String} to an
* {@code int}, as well as other constants and methods useful when
* dealing with an {@code int}.
* &p&Implementation note: The implementations of the "bit twiddling"
* methods (such as {@link #highestOneBit(int) highestOneBit} and
* {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
* based on material from Henry S. Warren, Jr.'s &i&Hacker's
* Delight&/i&, (Addison Wesley, 2002).
Lee Boynton
Arthur van Hoff
Josh Bloch
Joseph D. Darcy
public final class Integer extends Number implements Comparable&Integer& {
* A constant holding the minimum value an {@code int} can
* have, -2&sup&31&/sup&.
@Native public static final int
MIN_VALUE = 0x;
* A constant holding the maximum value an {@code int} can
* have, 2&sup&31&/sup&-1.
@Native public static final int
MAX_VALUE = 0x7fffffff;
* The {@code Class} instance representing the primitive type
* {@code int}.
@SuppressWarnings("unchecked")
public static final Class&Integer&
TYPE = (Class&Integer&) Class.getPrimitiveClass("int");
* All possible chars for representing a number as a String
* 所有可能被用来代表数字的字符
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
* Returns a string representation of the first argument in the
* radix specified by the second argument.
* &p&If the radix is smaller than {@code Character.MIN_RADIX}
* or larger than {@code Character.MAX_RADIX}, then the radix
* {@code 10} is used instead.
* &p&If the first argument is negative, the first element of the
* result is the ASCII minus character {@code '-'}
* ({@code '\u005Cu002D'}). If the first argument is not
* negative, no sign character appears in the result.
* &p&The remaining characters of the result represent the magnitude
* of the first argument. If the magnitude is zero, it is
* represented by a single zero character {@code '0'}
* ({@code '\u005Cu0030'}); otherwise, the first character of
* the representation of the magnitude will not be the zero
* character.
The following ASCII characters are used as digits:
* &blockquote&
{@code abcdefghijklmnopqrstuvwxyz}
* &/blockquote&
* These are {@code '\u005Cu0030'} through
* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
* {@code '\u005Cu007A'}. If {@code radix} is
* &var&N&/var&, then the first &var&N&/var& of these characters
* are used as radix-&var&N&/var& digits in the order shown. Thus,
* the digits for hexadecimal (radix 16) are
* {@code abcdef}. If uppercase letters are
* desired, the {@link java.lang.String#toUpperCase()} method may
* be called on the result:
* &blockquote&
{@code Integer.toString(n, 16).toUpperCase()}
* &/blockquote&
an integer to be converted to a string.
the radix to use in the string representation.
a string representation of the argument in the specified radix.
java.lang.Character#MAX_RADIX
java.lang.Character#MIN_RADIX
public static String toString(int i, int radix) {
if (radix & Character.MIN_RADIX || radix & Character.MAX_RADIX)
radix = 10;
if (radix == 10) {
return toString(i);
char buf[] = new char[33];
boolean negative = (i & 0);
int charPos = 32;
if (!negative) {
while (i &= -radix) {
buf[charPos--] = digits[-(i % radix)];
buf[charPos] = digits[-i];
if (negative) {
buf[--charPos] = '-';
return new String(buf, charPos, (33 - charPos));
* Returns a string representation of the first argument as an
* unsigned integer value in the radix specified by the second
* argument.
* &p&If the radix is smaller than {@code Character.MIN_RADIX}
* or larger than {@code Character.MAX_RADIX}, then the radix
* {@code 10} is used instead.
* &p&Note that since the first argument is treated as an unsigned
* value, no leading sign character is printed.
* &p&If the magnitude is zero, it is represented by a single zero
* character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
* the first character of the representation of the magnitude will
* not be the zero character.
* &p&The behavior of radixes and the characters used as digits
* are the same as {@link #toString(int, int) toString}.
an integer to be converted to an unsigned string.
the radix to use in the string representation.
an unsigned string representation of the argument in the specified radix.
#toString(int, int)
public static String toUnsignedString(int i, int radix) {
return Long.toUnsignedString(toUnsignedLong(i), radix);
* Returns a string representation of the integer argument as an
* unsigned integer in base&16.
* &p&The unsigned integer value is the argument plus 2&sup&32&/sup&
otherwise, it is equal to the
* argument.
This value is converted to a string of ASCII digits
* in hexadecimal (base&16) with no extra leading
* {@code 0}s.
* //如果传入的参数为负数,则将其加上2的32次方;否则不变。这个数值将被转换为十六进制,并且开头的0将被省去
* &p&The value of the argument can be recovered from the returned
* string {@code s} by calling {@link
* Integer#parseUnsignedInt(String, int)
* Integer.parseUnsignedInt(s, 16)}.
* //这个被转换的数可以通过调用Integer.parseUnsignedInt(s, 16)函数重新获得。
* &p&If the unsigned magnitude is zero, it is represented by a
* single zero character {@code '0'} ({@code '\u005Cu0030'});
* otherwise, the first character of the representation of the
* unsigned magnitude will not be the zero character. The
* following characters are used as hexadecimal digits:
* //除非这个数等于0,否则数的开头不会是0.接下来的数字将用十六进制的字符来表示,
* //如果希望字母为大写,可以调用String.toUpperCase()
* &blockquote&
{@code abcdef}
* &/blockquote&
* These are the characters {@code '\u005Cu0030'} through
* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
* {@code '\u005Cu0066'}. If uppercase letters are
* desired, the {@link java.lang.String#toUpperCase()} method may
* be called on the result:
* &blockquote&
{@code Integer.toHexString(n).toUpperCase()}
* &/blockquote&
an integer to be converted to a string.
the string representation of the unsigned integer value
represented by the argument in hexadecimal (base&16).
* #parseUnsignedInt(String, int)
* #toUnsignedString(int, int)
public static String toHexString(int i) {
return toUnsignedString0(i, 4);
* Returns a string representation of the integer argument as an
* unsigned integer in base&8.
* &p&The unsigned integer value is the argument plus 2&sup&32&/sup&
otherwise, it is equal to the
* argument.
This value is converted to a string of ASCII digits
* in octal (base&8) with no extra leading {@code 0}s.
* &p&The value of the argument can be recovered from the returned
* string {@code s} by calling {@link
* Integer#parseUnsignedInt(String, int)
* Integer.parseUnsignedInt(s, 8)}.
* &p&If the unsigned magnitude is zero, it is represented by a
* single zero character {@code '0'} ({@code '\u005Cu0030'});
* otherwise, the first character of the representation of the
* unsigned magnitude will not be the zero character. The
* following characters are used as octal digits:
* &blockquote&
* {@code }
* &/blockquote&
* These are the characters {@code '\u005Cu0030'} through
* {@code '\u005Cu0037'}.
an integer to be converted to a string.
the string representation of the unsigned integer value
represented by the argument in octal (base&8).
* #parseUnsignedInt(String, int)
* #toUnsignedString(int, int)
public static String toOctalString(int i) {
return toUnsignedString0(i, 3);
* Returns a string representation of the integer argument as an
* unsigned integer in base&2.
* &p&The unsigned integer value is the argument plus 2&sup&32&/sup&
otherwise it is equal to the
* argument.
This value is converted to a string of ASCII digits
* in binary (base&2) with no extra leading {@code 0}s.
* &p&The value of the argument can be recovered from the returned
* string {@code s} by calling {@link
* Integer#parseUnsignedInt(String, int)
* Integer.parseUnsignedInt(s, 2)}.
* &p&If the unsigned magnitude is zero, it is represented by a
* single zero character {@code '0'} ({@code '\u005Cu0030'});
* otherwise, the first character of the representation of the
* unsigned magnitude will not be the zero character. The
* characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
* '1'} ({@code '\u005Cu0031'}) are used as binary digits.
an integer to be converted to a string.
the string representation of the unsigned integer value
represented by the argument in binary (base&2).
* #parseUnsignedInt(String, int)
* #toUnsignedString(int, int)
public static String toBinaryString(int i) {
return toUnsignedString0(i, 1);
* Convert the integer to an unsigned number.将int类型的数字转换成一个无符号数
private static String toUnsignedString0(int val, int shift) {
int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
int chars = Math.max(((mag + (shift - 1)) / shift), 1);
char[] buf = new char[chars];
formatUnsignedInt(val, shift, buf, 0, chars);
return new String(buf, true);
* Format a long (treated as unsigned) into a character buffer.
* val the unsigned int to format 无符号整数的格式
* shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
* buf the character buffer to write to 字符数组缓冲区
* offset the offset in the destination buffer to start at 目标缓冲区的开始偏移量
* len the number of characters to write 写入的字符数
* the lowest character
location used 低位字符?
static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
int charPos =
int radix = 1 &&
int mask = radix - 1;
buf[offset + --charPos] = Integer.digits[val & mask];
} while (val != 0 && charPos & 0);
return charP
final static char [] DigitTens = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
final static char [] DigitOnes = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
* Returns a {@code String} object representing the
* specified integer. The argument is converted to signed decimal
* representation and returned as a string, exactly as if the
* argument and radix 10 were given as arguments to the {@link
* #toString(int, int)} method.
* 返回一个表示特定整数的字符串,参数被转换为十进制并作为字符串返回,就像调用方法toString(i, 10)效果一样
an integer to be converted.
a string representation of the argument in base&10.
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-";
int size = (i & 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
* Returns a string representation of the argument as an unsigned
* decimal value.
* The argument is converted to unsigned decimal representation
* and returned as a string exactly as if the argument and radix
* 10 were given as arguments to the {@link #toUnsignedString(int,
* int)} method.
an integer to be converted to an unsigned string.
an unsigned string representation of the argument.
#toUnsignedString(int, int)
public static String toUnsignedString(int i) {
return Long.toString(toUnsignedLong(i));
* Places characters representing the integer i into the
* character array buf. The characters are placed into
* the buffer backwards starting with the least significant
* digit at the specified index (exclusive), and working
* backwards from there.
* Will fail if i == Integer.MIN_VALUE 当i等于最小值时,将转换失败
static void getChars(int i, int index, char[] buf) {
int charPos =
char sign = 0;
if (i & 0) {
sign = '-';
while (i &= 65536) {
q = i / 100;
r = i - ((q && 6) + (q && 5) + (q && 2));
buf [--charPos] = DigitOnes[r];
buf [--charPos] = DigitTens[r];
for (;;) {
q = (i * 52429) &&& (16+3);
r = i - ((q && 3) + (q && 1));
buf [--charPos] = digits [r];
if (i == 0) break;
if (sign != 0) {
buf [--charPos] =
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
, , Integer.MAX_VALUE };
static int stringSize(int x) {
for (int i=0; ; i++)
if (x &= sizeTable[i])
return i+1;
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
* indicate a negative value or an ASCII plus sign {@code '+'}
* ({@code '\u005Cu002B'}) to indicate a positive value. The
* resulting integer value is returned.
* 将一个字符串参数解析为有符号整数(按照第二个参数给出的基数,就是进制)。这个字符串必须全部都由特定进制的表示数字的字符组成,
* (由java.lang.Character.digit(char, int)返回的一个非负值),否则第一个字符将是‘-’来表明负数或者‘+’来表明正数。最后返回得到的整数
* &p&An exception of type {@code NumberFormatException} is 当如下的其中一种情况出现时,将抛出异常
* thrown if any of the following situations occurs:
* &li&The first argument is {@code null} or is a string of
* length zero. 第一个参数为null或是一个长度为0的字符串
* &li&The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.代表进制的数超出规定范围
* &li&Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* {@code '-'} ({@code '\u005Cu002D'}) or plus sign
* {@code '+'} ({@code '\u005Cu002B'}) provided that the
* string is longer than length 1. 这个字符串中任意一个字符不是进制所规定数字的字符(除了第一个字符可以是‘-’和‘+’,且该字符串长度大于1)
* &li&The value represented by the string is not a value of type
* {@code int}. 这个字符串表示的数字不是int类型
* &p&Examples:
* &blockquote&&pre&
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("+42", 10) returns 42
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("", 10) returns
* parseInt("-", 10) returns -
* parseInt("", 10) throws a NumberFormatException 超出int可以表示的数字范围
* parseInt("99", 8) throws a NumberFormatException 99不是一个八进制数字
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787 二十七进制。。。
* &/pre&&/blockquote&
the {@code String} containing the integer
representation to be parsed
the radix to be used while parsing {@code s}.
the integer represented by the string argument in the
specified radix.
NumberFormatException if the {@code String}
does not contain a parsable {@code int}.
public static int parseInt(String s, int radix)
throws NumberFormatException
if (s == null) {
throw new NumberFormatException("null");
if (radix & Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
if (radix & Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
if (len & 0) {
char firstChar = s.charAt(0);
if (firstChar & '0') {
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1)
throw NumberFormatException.forInputString(s);
multmin = limit /
while (i & len) {
digit = Character.digit(s.charAt(i++),radix);
if (digit & 0) {
throw NumberFormatException.forInputString(s);
if (result & multmin) {
throw NumberFormatException、.forInputString(s);
if (result & limit + digit) {
throw NumberFormatException.forInputString(s);
throw NumberFormatException.forInputString(s);
return negative ? result : -
* Parses the string argument as a signed decimal integer. The
* characters in the string must all be decimal digits, except
* that the first character may be an ASCII minus sign {@code '-'}
* ({@code '\u005Cu002D'}) to indicate a negative value or an
* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
* indicate a positive value. The resulting integer value is
* returned, exactly as if the argument and the radix 10 were
* given as arguments to the {@link #parseInt(java.lang.String,
* int)} method.
a {@code String} containing the {@code int}
representation to be parsed
the integer value represented by the argument in decimal.
NumberFormatException
if the string does not contain a
parsable integer.
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
* Parses the string argument as an unsigned integer in the radix
* specified by the second argument.
An unsigned integer maps the
* values usually associated with negative numbers to positive
* numbers larger than {@code MAX_VALUE}.
* The characters in the string must all be digits of the
* specified radix (as determined by whether {@link
* java.lang.Character#digit(char, int)} returns a nonnegative
* value), except that the first character may be an ASCII plus
* sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
* integer value is returned.只允许有‘+’和对应进制的数字。
* &p&An exception of type {@code NumberFormatException} is
* thrown if any of the following situations occurs:
* &li&The first argument is {@code null} or is a string of
* length zero.
* &li&The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* &li&Any character of the string is not a digit of the specified
* radix, except that the first character may be a plus sign
* {@code '+'} ({@code '\u005Cu002B'}) provided that the
* string is longer than length 1.
* &li&The value represented by the string is larger than the
* largest unsigned {@code int}, 2&sup&32&/sup&-1.
the {@code String} containing the unsigned integer
representation to be parsed
the radix to be used while parsing {@code s}.
the integer represented by the string argument in the
specified radix.
NumberFormatException if the {@code String}
does not contain a parsable {@code int}.
public static int parseUnsignedInt(String s, int radix)
throws NumberFormatException {
if (s == null)
throw new NumberFormatException("null");
int len = s.length();
if (len & 0) {
char firstChar = s.charAt(0);
if (firstChar == '-') {
NumberFormatException(String.format("Illegal leading minus sign " +
"on unsigned string %s.", s));
if (len &= 5 ||
(radix == 10 && len &= 9) ) {
return parseInt(s, radix);
long ell = Long.parseLong(s, radix);
if ((ell & 0xffff_ffff_L) == 0) {
return (int)
NumberFormatException(String.format("String value %s exceeds " +
"range of unsigned int.", s));
throw NumberFormatException.forInputString(s);
* Parses the string argument as an unsigned decimal integer. The
* characters in the string must all be decimal digits, except
* that the first character may be an an ASCII plus sign {@code
* '+'} ({@code '\u005Cu002B'}). The resulting integer value
* is returned, exactly as if the argument and the radix 10 were
* given as arguments to the {@link
* #parseUnsignedInt(java.lang.String, int)} method.
a {@code String} containing the unsigned {@code int}
representation to be parsed
the unsigned integer value represented by the argument in decimal.
NumberFormatException
if the string does not contain a
parsable unsigned integer.
public static int parseUnsignedInt(String s) throws NumberFormatException {
return parseUnsignedInt(s, 10);
* Returns an {@code Integer} object holding the value
* extracted from the specified {@code String} when parsed
* with the radix given by the second argument. The first argument
* is interpreted as representing a signed integer in the radix
* specified by the second argument, exactly as if the arguments
* were given to the {@link #parseInt(java.lang.String, int)}
* method. The result is an {@code Integer} object that
* represents the integer value specified by the string.
* &p&In other words, this method returns an {@code Integer}
* object equal to the value of:
* &blockquote&
{@code new Integer(Integer.parseInt(s, radix))}
* &/blockquote&
the string to be parsed.
radix the radix to be used in interpreting {@code s}
an {@code Integer} object holding the value
represented by the string argument in the specified
* NumberFormatException if the {@code String}
does not contain a parsable {@code int}.
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
* Returns an {@code Integer} object holding the
* value of the specified {@code String}. The argument is
* interpreted as representing a signed decimal integer, exactly
* as if the argument were given to the {@link
* #parseInt(java.lang.String)} method. The result is an
* {@code Integer} object that represents the integer value
* specified by the string.
* &p&In other words, this method returns an {@code Integer}
* object equal to the value of:
* &blockquote&
{@code new Integer(Integer.parseInt(s))}
* &/blockquote&
the string to be parsed.
an {@code Integer} object holding the value
represented by the string argument.
NumberFormatException
if the string cannot be parsed
as an integer.
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
* Cache to support the object identity semantics of autoboxing (自动装箱) for values between
* -128 and 127 (inclusive) as required by JLS.
* The cache is initialized on first usage. 在第一次使用时初始化缓存 The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=&size&} option. 缓存区的大小可以由参数-XX:AutoBoxCacheMax设定
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class. 在虚拟机初始化时期,这个这个属性会被设置并保存在sun.misc.VM.calss的私人系统属性当中
private static class IntegerCache {
static final int low = -128;
static final int
static final Integer cache[];
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
cache = new Integer[(high - low) + 1];
for(int k = 0; k & cache. k++)
cache[k] = new Integer(j++);
assert IntegerCache.high &= 127;
private IntegerCache() {}
* Returns an {@code Integer} instance representing the specified
* {@code int} value.
If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
* 返回一个Integer实例。如果一个新的Integer实例不是必选项?,更倾向于使用这个方法通常而不是构造函数,因为
* 这是方法提供频繁请求的缓存值,有更好的空间和时间性能。
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* 这个方法总是可以缓存-128到127的数,当然也可以缓存这个范围之外的数
i an {@code int} value.
* an {@code Integer} instance representing {@code i}.
public static Integer valueOf(int i) {
if (i &= IntegerCache.low && i &= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
* The value of the {@code Integer}.
<span class="hljs-keyword"
17分钟前139阅17分钟前163阅1小时前81阅1小时前66阅1小时前87阅2小时前80阅4小时前161阅4小时前179阅4小时前70阅4小时前68阅
CentOS专题
951阅2481阅9261阅1336阅8544阅1304阅5331阅8957阅5468阅1184阅
5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!
按 Ctrl+D 键,
把本文加入收藏夹
热门栏目 &
热门教程 &
图书推荐 &
12345678910
12345678910
12345678910

我要回帖

更多关于 java tostring方法 的文章

 

随机推荐