理解指定的System.out.println()

文章详细分析了Java中System.out.println()的源码,包括System类的性质,out静态变量的定义,以及println()方法的功能。println()是PrintStream类的一个方法,用于向控制台输出信息并换行。它会自动调用参数的toString()方法。文章还举例说明了不同类型的参数如何被打印,并提到了print方法的区别在于不进行换行操作。
摘要由CSDN通过智能技术生成

如何理解指定的System.out.println()

1 源码分析

1.1 System部分源码的分析


/**
 * The <code>System</code> class contains several useful class fields
 * and methods. It cannot be instantiated.
 *
 * <p>Among the facilities provided by the <code>System</code> class
 * are standard input, standard output, and error output streams;
 * access to externally defined properties and environment
 * variables; a means of loading files and libraries; and a utility
 * method for quickly copying a portion of an array.
 *
 * @author  unascribed
 * @since   JDK1.0
 */

//System 是 java.lang类包中的一个,其中注意的是final的存在

public final class System {

    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }
    
    ...
}

1.2 out 源码的分析

public final static PrintStream out = null;

// 分析:其中的final static 修饰之后不能够改变类的方法,out是对象
/**
*  1.out 是System中的一个静态的数据成员,而且这个成员是 java.io.PrintStream 类的引用
*  2.out通过static 修饰后,所以能够直接的使用类名 + 属性名称的方式进行调用,也就是System.out
*  3.out的真实的类型实际上是PrintStream 对象,因为是静态的,所以不需要创建爱你任何的东西,既不需要创建System类的对象。
*/

1.3 println源码的分析

  1. println() 就是 java.io.PrintStream 类中的一个方法,它的作用是向控制台输出信息
  2. 里面有很多的重载的方法,这样就保证了任意的东西都进行输出

/**
     * Prints an Object and then terminate the line.  This method calls
     * at first String.valueOf(x) to get the printed object's string value,
     * then behaves as
     * though it invokes <code>{@link #print(String)}</code> and then
     * <code>{@link #println()}</code>.
     *
     * @param x  The <code>Object</code> to be printed.
*/

/**
*  该方法的参数的类型实际上就是Object的类型,能够适用于多个类型
*/
public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) {
        print(s);
        newLine();
    }
}

2. 总结

2.1 关于println()方法

  1. 对于该方法的重载,存在多种的格式,其中有的是对于基本数据类型、String数据类型的输出的方式、以及Object类型的输出结果类型。
  2. java打印的输出,会自动调用参数的toString方法,输出内容时的toString方法的返回值。

public class SystemTest {
    public static void main(String[] args) {
        char[] chars = {'X', 'Y'};
        System.out.println("char1"+chars);
    }
}

//运行的结果:
char1[C@6d6f6e28
      // 分析:对于输出的格式,因为加上的是双引号,所以在调用的时候,自动调用println(String),也就是String类型,输出的格式是xxxx@XXXXX的格式

2.2 关于print方法

public void print(String s) {
    if (s == null) {
        s = "null";
    }
    write(s);
}
// 表示的是输出需要的内容,不进行换行的操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值