厦门大学 發表於 2025-7-22 18:36:00

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

<p><code>System.out.println(Object)</code> 是 Java 中最常用的输出方法之一,它的行为等同于 <code>java.io.PrintStream#println(Object)</code>,因为 <code>System.out</code> 是一个 PrintStream 实例。</p>
<pre><code class="language-java">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.
   * &lt;p&gt;
   * For simple stand-alone Java applications, a typical way to write
   * a line of output data is:
   * &lt;blockquote&gt;&lt;pre&gt;
   *   System.out.println(data)
   * &lt;/pre&gt;&lt;/blockquote&gt;
   * &lt;p&gt;
   * See the &lt;code&gt;println&lt;/code&gt; methods in class &lt;code&gt;PrintStream&lt;/code&gt;.
   *
   * @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;
}
</code></pre>
<br>
<p><code>java.io.PrintStream#println(Object)</code> 方法会打印对象的 <strong>字符串表示形式</strong>,其行为由 Java 语言规范定义,具体实现如下:</p>
<pre><code class="language-java">// class java.io.PrintStream

public void println(Object x) {
    String s = String.valueOf(x); // 关键转换
    synchronized (this) {
      print(s);
      newLine(); // 添加换行符
    }
}
</code></pre>
<p>其中,<code>java.lang.String#valueOf(Object)</code> 描述了对象转字符串的机制。</p>
<pre><code class="language-java">// class java.lang.String

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}
</code></pre>
<p>继续来看<code>Object.toString</code>方法,该方法返回一个<code>类名@十六进制哈希码</code>串。见如下java源码:</p>
<pre><code class="language-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.
* &lt;p&gt;
* 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:
* &lt;blockquote&gt;
* &lt;pre&gt;
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* &lt;/pre&gt;&lt;/blockquote&gt;
*
* @returna string representation of the object.
*/
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
</code></pre>
<br>
<h2 id="示例代码演示">示例代码演示</h2>
<ol>
<li>下面是默认打印方式</li>
</ol>
<pre><code class="language-java">// 普通对象(未重写 toString)
Object obj = new Object();
System.out.println(obj);   // 输出: java.lang.Object@6d06d69c
</code></pre>
<ol start="2">
<li>如果需要友好打印,可以重新 toString 方法</li>
</ol>
<pre><code class="language-java">// 重写 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
</code></pre>
<ol start="3">
<li>关于 lombok的 <code>@ToString</code> 注解</li>
</ol>
<p>lombok工具会为class类生成 toString 方法。见下方示例:</p>
<pre><code class="language-java">@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 + ")";
    }
}
</code></pre>
<p><br><br><br>
anyway,生产环境要严谨使用System.out.println来打印对象,PrintStream是同步的,会影响程序性能,尤其是多线程环境下会成为性能瓶颈。相比之下,我们借助更具优势的日志框架,例如SLF4J+Logback组合,来实现程序日志的打印。</p>
<h3 id="日志框架的优势">日志框架的优势:</h3>
<ol>
<li><strong>异步输出</strong>:通过AsyncAppender实现非阻塞日志</li>
<li><strong>级别控制</strong>:动态调整日志级别(TRACE/DEBUG/INFO/WARN/ERROR)</li>
<li><strong>性能优化</strong>:使用占位符延迟字符串构建</li>
<li><strong>格式定制</strong>:自定义日志格式(时间戳、线程ID等)</li>
</ol>


</div>
<div id="MySignature" role="contentinfo">
    <hr class="signhr"><p style="text-indent:2em;font-size:12px;text-align:center">当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge<br>本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/18999300</p><hr class="signhr">
<style>hr.signhr{width:80%;margin:0 auto;border: 0;height: 4px;background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0))}</style><br><br>
来源:https://www.cnblogs.com/buguge/p/18999300
頁: [1]
查看完整版本: `System.out.println(Object)` 的打印行为详解