`System.out.println(Object)` 的打印行为详解

package java.lang;

public final class System {

    /**
     * The "standard" output stream. This stream is already
     * open and ready to accept output data. Typically this stream
     * corresponds to display output or another output destination
     * specified by the host environment or user.
     * <p>
     * For simple stand-alone Java applications, a typical way to write
     * a line of output data is:
     * <blockquote><pre>
     *     System.out.println(data)
     * </pre></blockquote>
     * <p>
     * See the <code>println</code> methods in class <code>PrintStream</code>.
     *
     * @see     java.io.PrintStream#println()
     * @see     java.io.PrintStream#println(boolean)
     * @see     java.io.PrintStream#println(char)
     * @see     java.io.PrintStream#println(char[])
     * @see     java.io.PrintStream#println(double)
     * @see     java.io.PrintStream#println(float)
     * @see     java.io.PrintStream#println(int)
     * @see     java.io.PrintStream#println(long)
     * @see     java.io.PrintStream#println(java.lang.Object)
     * @see     java.io.PrintStream#println(java.lang.String)
     */
    public final static PrintStream out = null;
}

java.io.PrintStream#println(Object) 方法会打印对象的 字符串表示形式,其行为由 Java 语言规范定义,具体实现如下:

// class java.io.PrintStream

public void println(Object x) {
    String s = String.valueOf(x); // 关键转换
    synchronized (this) {
        print(s);
        newLine(); // 添加换行符
    }
}

其中,java.lang.String#valueOf(Object) 描述了对象转字符串的机制。

// class java.lang.String

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

继续来看Object.toString方法,该方法返回一个类名@十六进制哈希码串。见如下java源码:

// class 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());
}

示例代码演示

  1. 下面是默认打印方式
// 普通对象(未重写 toString)
Object obj = new Object();
System.out.println(obj);   // 输出: java.lang.Object@6d06d69c
  1. 如果需要友好打印,可以重新 toString 方法
// 重写 toString 的对象
class Person {
    String name;
    Person(String name) { this.name = name; }
    @Override
    public String toString() { return "Person: " + name; }
}

System.out.println(new Person("Alice")); // 输出: Person: Alice
  1. 关于 lombok的 @ToString 注解

lombok工具会为class类生成 toString 方法。见下方示例:

@ToString
public class Person {
    int age;
    String name;
    
    Person(String name) { this.name = name; }
}

// 上面class经过IDE build以后,会生成如下代码中的 toString方法

public class Person {
    int age;
    String name;

    Person(String name) {
        this.name = name;
    }

    public String toString() {
        return "Person(age=" + this.age + ", name=" + this.name + ")";
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值