2012-08-17 53 views
35

可能重复:
What's the meaning of System.out.println in Java?什么是系统的,在的println的System.out.println()在Java中

我一直在寻找的答案是什么Systemout和在Java中,printlnSystem.out.println()中。我搜查,发现了不同的答案这样的:

  • 系统是一个内置类存在于java.lang包。 该类有一个final修饰符,这意味着它不能被其他类继承。 它包含预先定义的方法和字段,其提供设施,如标准输入,输出等

  • 出是在系统类的静态最终字段(即,变量),它是类型为PrintStream的(一个内置类,包含打印不同数据值的方法)。 必须使用类名来访问静态字段和方法,所以(System.out)。

  • 在这里表示类型为PrintStream类的参考变量。

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

在另一个页面我找到另一个对比定义为

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

我很困惑这些。任何人都可以请告诉我他们是什么?

+1

请参阅http://stackoverflow.com/q/3406703/396730和http://stackoverflow.com/q/10004856/396730 – 2012-08-17 08:18:18

+0

我认为第一种解释更清晰一些,您可以随时浏览源代码以查看对于你自己,例如'public final static PrintStream out = nullPrintStream();' – Less 2012-08-17 08:18:48

+8

第二种解释显然是错误的。 – 2012-08-17 08:19:32

回答

7

您发布的第一个答案(系统是一个内置类...)是漂亮的地方。

您可以补充说明System类包含大部分是本地的,并且由JVM在启动过程中设置,例如将System.out printstream连接到与“标准输出”(控制台)关联的本机输出流。

11

每当你感到困惑时,我建议咨询Javadoc作为你澄清的第一位。

根据JavaDoc约System,这里的医生说什么:

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) 
+1

当你明白他们的意思时,咨询Javadoc是很好的。但如果你不这样做?例如,新手可能不理解标准输出和错误输出流的概念,并且流已经打开并准备好接受输出数据。当然,新手可能会为这个概念提供谷歌搜索,但这往往会导致他无法理解的新概念。我没有告诉Javadocs有什么问题。我只是指出新手可能遇到的困难。这就是为什么他经常转而采用一种他可以忽略的语言来解释。 – 2016-01-07 11:46:45

+0

@BobUeland:当你不了解某些事情时,IMO最好的办法是看看它的文档,而不是看着人们对同一事物的解释。当人们用自己的语言解读官方文件时,有时可能会成为混淆本身。我的意思是有很多方法可以用你自己的话来解释同样的事情,如果有大量的答案,那么混淆本身的可能性非常高。 – Sujay 2016-01-07 20:09:12

+0

我同意你的看法,但不懂文档的新手想要/需要这样的东西:“看看System.out.println()。第一个以大写字母开头,所以它是一个类。第三个是()所以这是一个方法,第二个以小写字母开头,没有(),所以它是一个字段,你想打印一些东西,但是必须找到能为你做这个工作的人,你不知道那个人,你知道系统有一个叫做出来的字段,指向这个人,所以你知道System.out对这个人有用,并且你知道这个人有println()方法。 – 2016-01-08 11:24:54

102

System是来自java.lang包的最终课程。

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

printlnPrintStream类的一种方法。

+11

这应该是正确答案 – OMGPOP 2013-09-02 07:12:40

相关问题