What's the meaning of System.out.println in Ja

2019-01-05 10:24发布

Is this static println function in out class from System namespace?

namespace System {
  class out {
    static println ...
  }

How can I interpret this name? And where in JRE this function is defined? In java.lang.System/java.lang.Object?

20条回答
男人必须洒脱
2楼-- · 2019-01-05 10:50

From the javadoc about System, here's what the doc says:

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

Regarding System.out

public static final PrintStream out

The "standard" output stream class Prinstream  belongs to java.io package. This stream is already open and ready to accept output data. 
When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s name says – it initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().
Typically this stream corresponds to display output or another output destination specified by the host environment or user.

Regarding println();

class PrintStream{
public void println();
}

For simple stand-alone Java applications, a typical way to write a line of output data is:

System.out.println(data);
查看更多
Juvenile、少年°
3楼-- · 2019-01-05 10:50

System: is predefined class of java.lang package.

out: is a static member of printStream class and its connect with console.

Println: is a method of printstream class and its not a static.

查看更多
SAY GOODBYE
4楼-- · 2019-01-05 10:51
System.out.println();

System is the class

out is a variable in the System class and it is a static and variable type is PrintStream.

Here is the out variable in System class:

public final static PrintStream out = null;

You can see implementation of System here.

println() is a overloaded method in PrintStream class.

PrintStream includes three overloaded printing methods, those are:

  1. print()
  2. println()
  3. printf()

You can see implementation of PrintStream here.

You cannot instantiate System class and it is child class of Object and the Object is the father(superclass) of every classes including classes that you defined.

Here is what the oracle docs says:

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

If you donot know what is meant by instantiate, read this questioh. It is C# question but the concept is same.

Also, What is the difference between an Instance and an Object?

If you donot know what is meant by overload read this quesiotn.

查看更多
萌系小妹纸
5楼-- · 2019-01-05 10:52

Check following link:

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html

You will clearly see that:

System is a class in the java.lang package.

out is a static member of the System class, and is an instance of java.io.PrintStream.

println is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-05 10:55

System is a class, that has a public static field out. So it's more like

class System 
{
    public static PrintStream out;
}

class PrintStream
{
    public void println ...
}

This is a slight oversimplification, as the PrintStream class is actually in the java.io package, but it's good enough to show the relationship of stuff.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-05 10:57
System.out.println("Hello World");
  1. System: It is the name of standard class that contains objects that encapsulates the standard I/O devices of your system.

It is contained in the package java.lang. Since java.lang package is imported in every java program by default,therefore java.lang package is the only package in Java API which does not require an import declaration.

  1. out:The object out represents output stream(i.e Command window)and is the static data member of the class System.

So note here System.out (System -Class & out- static object i.e why its simply referred to by classname and we need not create any object).

  1. println:The println() is method of out object that takes the text string as an argument and displays it to the standard output i.e on monitor screen.

Note
System -Class
out -static Object
println() -method
Remember a function (in java function is called method) always has the format function()

查看更多
登录 后发表回答