java的toString()及包装类的实现--Integer重点学习

时间:2020-12-15 15:06:14

1. toString()来源

2. toString()目的

3. toString()实现(JDK8)

1. toString()来源

  源于java.lang.Object类,源码如下:

    /**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

2. toString()目的

  返回对象的字符串描述,是该对象的文本表示。该表示必须描述简洁且包含了该对象的信息,便于阅读。

  推荐所有的子类重写该方法。

3. toString()实现(JDK8)

(1)Object类中的实现

  在Object类中,该方法返回的是“该实例对象的类名@该对象的hashcode的十六进制表示”。对于我们而言,能阅读到的仅仅是该实例的类名。所以推荐对Object的子类重写该方法。

(2)包装类中的实现

 ① Boolean

    /**
* Returns a {@code String} object representing this Boolean's
* value. If this object represents the value {@code true},
* a string equal to {@code "true"} is returned. Otherwise, a
* string equal to {@code "false"} is returned.
*
* @return a string representation of this object.
*/
public String toString() {
return value ? "true" : "false";
}

② Byte(借助于Integer)

    /**
* Returns a new {@code String} object representing the
* specified {@code byte}. The radix is assumed to be 10.
*
* @param b the {@code byte} to be converted
* @return the string representation of the specified {@code byte}
* @see java.lang.Integer#toString(int)
*/
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
/**
* Returns a {@code String} object representing this
* {@code Byte}'s value. The value is converted to signed
* decimal representation and returned as a string, exactly as if
* the {@code byte} value were given as an argument to the
* {@link java.lang.Byte#toString(byte)} method.
*
* @return a string representation of the value of this object in
* base&nbsp;10.
*/
public String toString() {
return Integer.toString((int)value);
}

③ Short(借助于Integer)

    /**
* Returns a new {@code String} object representing the
* specified {@code short}. The radix is assumed to be 10.
*
* @param s the {@code short} to be converted
* @return the string representation of the specified {@code short}
* @see java.lang.Integer#toString(int)
*/
public static String toString(short s) {
return Integer.toString((int)s, 10);
}
/**
* Returns a {@code String} object representing this
* {@code Short}'s value. The value is converted to signed
* decimal representation and returned as a string, exactly as if
* the {@code short} value were given as an argument to the
* {@link java.lang.Short#toString(short)} method.
*
* @return a string representation of the value of this object in
* base&nbsp;10.
*/
public String toString() {
return Integer.toString((int)value);
}

④ Character

    /**
* Returns a {@code String} object representing this
* {@code Character}'s value. The result is a string of
* length 1 whose sole component is the primitive
* {@code char} value represented by this
* {@code Character} object.
*
* @return a string representation of this object.
*/
public String toString() {
char buf[] = {value}; // 转换为数组,可能和code point有关
return String.valueOf(buf);
}
https://blog.csdn.net/qlql489/article/details/82780716

⑤ Integer

    /**
* 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.
* 如果进制参数不再Character的可表示的进制范围内,设为十进制 * <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:
* 结果的剩余字符串表示了第一个参数的大小,如果参数为0,则返回‘0’,否则字符串的第一个字符不为0。使用ASCII字符:0-9a-z * <blockquote>
* {@code 0123456789abcdefghijklmnopqrstuvwxyz}
* </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 0123456789abcdef}. 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>
*
* @param i an integer to be converted to a string.
* @param radix the radix to use in the string representation.
* @return a string representation of the argument in the specified radix.
* @see java.lang.Character#MAX_RADIX
* @see java.lang.Character#MIN_RADIX
*/
public static String toString(int i, int radix) {
// 进制范围
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10; /* Use the faster version */
if (radix == 10) {
return toString(i);
} // 除十进制外的其他进制
// 返回的字符串,最长为33个字符(32个二进制+1个符号)
char buf[] = new char[33];
// 记录正负
boolean negative = (i < 0);
int charPos = 32;
// 保证i<=0
if (!negative) {
i = -i;
}
// 从后往前转字符 123->3,2,1
while (i <= -radix) {
buf[charPos--] = digits[-(i % radix)];
i = i / radix;
}
// 最高位
buf[charPos] = digits[-i];
// 符号位
if (negative) {
buf[--charPos] = '-';
}
// 返回不为0的字符部分
return new String(buf, charPos, (33 - charPos));
}
/**
* 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.
*
* @param i an integer to be converted.
* @return a string representation of the argument in base&nbsp;10.
*/
// 十进制
public static String toString(int i) {
// 无法得到最小值的绝对值(超范围)
if (i == Integer.MIN_VALUE)
return "-2147483648";
// 需要的字符个数
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}
// Requires positive x
// 该十进制表示需要的位数(不包括符号)
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999,
9999999,99999999, 999999999, Integer.MAX_VALUE };
/**
* 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 最小值的绝对值超范围
*/
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index;
char sign = 0; if (i < 0) {
sign = '-';
i = -i;
} // Generate two digits per iteration
// 一次迭代产生两位字符,从低到高
while (i >= 65536) {
q = i / 100;
// really: r = i - (q * 100);
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
buf [--charPos] = DigitOnes[r];
buf [--charPos] = DigitTens[r];
} // Fall thru to fast mode for smaller numbers
// assert(i <= 65536, i);
// 以65536为分界线,下面使用移位和乘法的形式提高速度,
// 所以(i*52429)不能超过Integer.MAX_VALUE,2^15 < 52429 < 2^16,所以i<2^16,所以这里i的值不能超过65536

for (;;) {
// 2^19 = 524288,52429>>>19=0.1xxx
// 移位最快,乘 > 除
q = (i * 52429) >>> (16+3); // q = i/10;
r = i - ((q << 3) + (q << 1)); //
r = i-(q*10) ...
buf [--charPos] = digits [r];
i = q;
if (i == 0) break;
}
if (sign != 0) {
buf [--charPos] = sign;
}
}
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',
} ;
// 10x10
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 this
* {@code Integer}'s value. The value is converted to signed
* decimal representation and returned as a string, exactly as if
* the integer value were given as an argument to the {@link
* java.lang.Integer#toString(int)} method.
*
* @return a string representation of the value of this object in
* base&nbsp;10.
*/
public String toString() {
return toString(value);
}
两个toString,一个是在多进制下转字符串,有多少位就要循环多少次。
toString(value)的方法默认为十进制,毕竟使用的多,而上面的方法太慢了,toString(value)明显减少了迭代次数。

⑥ Long(同Integer)

   /**
* 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 sign {@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 0123456789abcdefghijklmnopqrstuvwxyz}
* </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 0123456789abcdef}. If uppercase letters are
* desired, the {@link java.lang.String#toUpperCase()} method may
* be called on the result:
*
* <blockquote>
* {@code Long.toString(n, 16).toUpperCase()}
* </blockquote>
*
* @param i a {@code long} to be converted to a string.
* @param radix the radix to use in the string representation.
* @return a string representation of the argument in the specified radix.
* @see java.lang.Character#MAX_RADIX
* @see java.lang.Character#MIN_RADIX
*/
public static String toString(long i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
if (radix == 10)
return toString(i);
char[] buf = new char[65];
int charPos = 64;
boolean negative = (i < 0); if (!negative) {
i = -i;
} while (i <= -radix) {
buf[charPos--] = Integer.digits[(int)(-(i % radix))];
i = i / radix;
}
buf[charPos] = Integer.digits[(int)(-i)]; if (negative) {
buf[--charPos] = '-';
} return new String(buf, charPos, (65 - charPos));
}
/**
* Returns a {@code String} object representing the specified
* {@code long}. The argument is converted to signed decimal
* representation and returned as a string, exactly as if the
* argument and the radix 10 were given as arguments to the {@link
* #toString(long, int)} method.
*
* @param i a {@code long} to be converted.
* @return a string representation of the argument in base&nbsp;10.
*/
public static String toString(long i) {
if (i == Long.MIN_VALUE)
return "-9223372036854775808";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}
/**
* 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 == Long.MIN_VALUE
*/
static void getChars(long i, int index, char[] buf) {
long q;
int r;
int charPos = index;
char sign = 0; if (i < 0) {
sign = '-';
i = -i;
} // Get 2 digits/iteration using longs until quotient fits into an int
while (i > Integer.MAX_VALUE) {
q = i / 100;
// really: r = i - (q * 100);
r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
i = q;
buf[--charPos] = Integer.DigitOnes[r];
buf[--charPos] = Integer.DigitTens[r];
} // Get 2 digits/iteration using ints
int q2;
int i2 = (int)i;
while (i2 >= 65536) {
q2 = i2 / 100;
// really: r = i2 - (q * 100);
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
i2 = q2;
buf[--charPos] = Integer.DigitOnes[r];
buf[--charPos] = Integer.DigitTens[r];
} // Fall thru to fast mode for smaller numbers
// assert(i2 <= 65536, i2);
for (;;) {
q2 = (i2 * 52429) >>> (16+3);
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
buf[--charPos] = Integer.digits[r];
i2 = q2;
if (i2 == 0) break;
}
if (sign != 0) {
buf[--charPos] = sign;
}
} // Requires positive x
static int stringSize(long x) {
long p = 10;
for (int i=1; i<19; i++) {
if (x < p)
return i;
p = 10*p;
}
return 19;
}
/**
* Returns a {@code String} object representing this
* {@code Long}'s value. The value is converted to signed
* decimal representation and returned as a string, exactly as if
* the {@code long} value were given as an argument to the
* {@link java.lang.Long#toString(long)} method.
*
* @return a string representation of the value of this object in
* base&nbsp;10.
*/
public String toString() {
return toString(value);
}

⑦ Float

    /**
* Returns a string representation of the {@code float}
* argument. All characters mentioned below are ASCII characters.
* <ul>
* <li>If the argument is NaN, the result is the string
* "{@code NaN}".
* <li>Otherwise, the result is a string that represents the sign and
* magnitude (absolute value) of the argument. If the sign is
* negative, the first character of the result is
* '{@code -}' ({@code '\u005Cu002D'}); if the sign is
* positive, no sign character appears in the result. As for
* the magnitude <i>m</i>:
* <ul>
* <li>If <i>m</i> is infinity, it is represented by the characters
* {@code "Infinity"}; thus, positive infinity produces
* the result {@code "Infinity"} and negative infinity
* produces the result {@code "-Infinity"}.
* <li>If <i>m</i> is zero, it is represented by the characters
* {@code "0.0"}; thus, negative zero produces the result
* {@code "-0.0"} and positive zero produces the result
* {@code "0.0"}.
* <li> If <i>m</i> is greater than or equal to 10<sup>-3</sup> but
* less than 10<sup>7
</sup>, then it is represented as the
* integer part of <i>m</i>, in decimal form with no leading
* zeroes, followed by '{@code .}'
* ({@code '\u005Cu002E'}), followed by one or more
* decimal digits representing the fractional part of
* <i>m</i>.
10^-3<= float < 10^7 : 整数.小数形式(1025.35)
float < 10^-3 || float >= 10^7 :科学计数法(1.024E7)
* <li> If <i>m</i> is less than 10<sup>-3</sup> or greater than or
* equal to 10<sup>7</sup>, then it is represented in
* so-called "computerized scientific notation." Let <i>n</i>
* be the unique integer such that 10<sup><i>n</i> </sup>&le;
* <i>m</i> {@literal <} 10<sup><i>n</i>+1</sup>; then let <i>a</i>
* be the mathematically exact quotient of <i>m</i> and
* 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10.
* The magnitude is then represented as the integer part of
* <i>a</i>, as a single decimal digit, followed by
* '{@code .}' ({@code '\u005Cu002E'}), followed by
* decimal digits representing the fractional part of
* <i>a</i>, followed by the letter '{@code E}'
* ({@code '\u005Cu0045'}), followed by a representation
* of <i>n</i> as a decimal integer, as produced by the
* method {@link java.lang.Integer#toString(int)}.
*
* </ul>
* </ul>
* How many digits must be printed for the fractional part of
* <i>m</i> or <i>a</i>? There must be at least one digit
* to represent the fractional part, and beyond that as many, but
* only as many, more digits as are needed to uniquely distinguish
* the argument value from adjacent values of type
* {@code float}. That is, suppose that <i>x</i> is the
* exact mathematical value represented by the decimal
* representation produced by this method for a finite nonzero
* argument <i>f</i>. Then <i>f</i> must be the {@code float}
* value nearest to <i>x</i>; or, if two {@code float} values are
* equally close to <i>x</i>, then <i>f</i> must be one of
* them and the least significant bit of the significand of
* <i>f</i> must be {@code 0}.
*
* <p>To create localized string representations of a floating-point
* value, use subclasses of {@link java.text.NumberFormat}.
*
* @param f the float to be converted.
* @return a string representation of the argument.
*/
public static String toString(float f) {
return FloatingDecimal.toJavaFormatString(f);
}
/**
* Returns a string representation of this {@code Float} object.
* The primitive {@code float} value represented by this object
* is converted to a {@code String} exactly as if by the method
* {@code toString} of one argument.
*
* @return a {@code String} representation of this object.
* @see java.lang.Float#toString(float)
*/
public String toString() {
return Float.toString(value);
}

⑧ Double

  /**
* <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
* than 10<sup>
</sup>, then it is represented as the integer part of
* <i>m</i>, in decimal form with no leading zeroes, followed by
* '{@code .}' ({@code '\u005Cu002E'}), followed by one or
* more decimal digits representing the fractional part of <i>m</i>.
*
* <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
* equal to 10<sup>
</sup>, then it is represented in so-called
* "computerized scientific notation." Let <i>n</i> be the unique
* integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
* 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
* mathematically exact quotient of <i>m</i> and
* 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
* magnitude is then represented as the integer part of <i>a</i>,
* as a single decimal digit, followed by '{@code .}'
* ({@code '\u005Cu002E'}), followed by decimal digits
* representing the fractional part of <i>a</i>, followed by the
* letter '{@code E}' ({@code '\u005Cu0045'}), followed
* by a representation of <i>n</i> as a decimal integer, as
* produced by the method {@link Integer#toString(int)}.
*/
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}

⑨ String

/**
* This object (which is already a string!) is itself returned.
*
* @return the string itself.
*/
public String toString() {
return this;
}