What's the meaning of System.out.println in Java?

后端 未结 19 758
谎友^
谎友^ 2020-11-27 11:49

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

namespace System {
  class out {
    static println ...         


        
相关标签:
19条回答
  • 2020-11-27 12:33

    System is the java class.

    out is the instance and also static member of PrintStream.

    println is the method of PrintStream.

    0 讨论(0)
  • 2020-11-27 12:33
    System.out.println
    

    System is a class in the java.lang package.

    out is a static data member of the System class and references a variable of the PrintStream class.

    0 讨论(0)
  • 2020-11-27 12:37

    System is a class in java.lang package

    out is a static object of PrintStream class in java.io package

    println() is a method in the PrintStream class

    0 讨论(0)
  • 2020-11-27 12:37

    It is quite simple to understand the question, but to answer it we need to dig deeper in to Java native code.

    • System is static class and cannot be instantiated
    • out is a reference variable defined in System
    • println() is the method used to print on standard output.

    A brief and nice explanation is always welcome on this as we can learn much from this single line of statement itself!

    0 讨论(0)
  • 2020-11-27 12:40

    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);
    
    0 讨论(0)
  • 2020-11-27 12:43

    System.out.println()

    High level Understanding

    For understanding this we need to recall few basics of java:

    • dot (.) operator in java: In java . (dot operator) is used only to call methods or variables. So we can say out is either method or variable.
    • Methods in java : we know methods always have parenthesis ‘( )’ after method name, So out cannot be a method in java. So out its a variable and println() is a method.
    • Class name in java: Class name should start with Capital letter ideally in java, So System is a class.

    Now with basic knowledge of java we know :

    • System is a Class
    • out is a Variable
    • println() is a method

    Lets get more in details:

    out variable: static or instance?

    • called using class name, so we know its static variable of System class.

    • but its calling a method println() method so ‘out’ is an object of the reference type PrintStream.

    the System class belongs to java.lang package

    class System {
      public static final PrintStream out;
      //...
    }
    

    the Prinstream class belongs to java.io package

    class PrintStream{
    public void println();
    //...
    }
    
    0 讨论(0)
提交回复
热议问题