Java toString() 方法

Java  Character類Java Character類


toString() 方法用于返回一個(gè)表示指定 char 值的 String 對(duì)象。結(jié)果是長度為 1 的字符串,僅由指定的 char 組成。

所有的java對(duì)象都會(huì)從最高層的基類Object那里繼承toString方法(Object類是java中所有類的父類)。

源代碼


    /**

     * 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());

    }


語法

String toString(char ch)

參數(shù)

  • ch -- 要轉(zhuǎn)換的字符。

返回值

返回指定 char 值的字符串表示形式。

實(shí)例

public class Test {

	public static void main(String args[]) {
		System.out.println(Character.toString('a'));
		System.out.println(Character.toString('A'));
	}
}

以上程序執(zhí)行結(jié)果為:

a
A

Java  Character類Java Character類