39

可能重复:
Java 中 System.out.println 的含义是什么?

我一直在寻找什么的答案Systemout并且printlnSystem.out.println()Java 中。我搜索并找到了一个不同的答案,如下所示:

  • System 是 java.lang 包中的一个内置类。这个类有一个final修饰符,这意味着它不能被其他类继承。它包含预定义的方法和字段,提供标准输入、输出等功能。

  • out 是 System 类中的静态 final 字段(即变量),属于 PrintStream 类型(内置类,包含打印不同数据值的方法)。静态字段和方法必须使用类名来访问,所以( System.out )。

  • 这里的 out 表示类型 PrintStream 类的引用变量。

  • println() 是 PrintStream 类中用于打印数据值的公共方法。因此,要访问 PrintStream 类中的方法,我们使用 out.println() (因为非静态方法和字段只能通过使用 refrence 变量来访问)

在另一页中,我发现另一个对比定义为

System.out.print 是 java 中使用的标准输出函数。其中 System 指定包名,out 指定类名,print 是该类中的函数。

我对这些感到困惑。有人能准确告诉我它们是什么吗?

4

3 回答 3

114

Systemjava.lang包中的最后一个类。

out是在类中PrintStream声明的类型的类变量System

printlnPrintStream类的方法。

于 2012-08-17T08:23:00.810 回答
11

每当您感到困惑时,我建议您首先查阅Javadoc以进行澄清。

从 javadoc aboutSystem中,文档是这样说的:

public final class System
extends Object

The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System 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.

Since:
JDK1.0

关于System.out

public static final PrintStream out
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.
For simple stand-alone Java applications, a typical way to write a line of output data is:

     System.out.println(data)
于 2012-08-17T08:22:22.883 回答
8

您发布的第一个答案(系统是内置类...)非常准确。

您可以添加System该类包含大部分是本机的并且由 JVM 在启动期间设置,例如将System.out打印流连接到与“标准输出”(控制台)关联的本机输出流。

于 2012-08-17T08:22:30.217 回答