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条回答
\"骚年 ilove
2楼-- · 2019-01-05 10:58

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!

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

System is a class.

The out variable (referred to above as System.out) is a class variable of the System class.

The PrintStream class is in a package of classes that are used to provide stream I/O capability for Java.

The out variable in the System class refers to (points to) an instance of the PrintStream class (a PrintStream object) which is automatically instantiated when the System class is loaded into the application. The PrintStream class has an instance method named println() which causes its argument to be displayed on the standard output device.

查看更多
该账号已被封号
4楼-- · 2019-01-05 10:59

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();
//...
}
查看更多
爷、活的狠高调
5楼-- · 2019-01-05 11:00

println and print are the two overloaded methods which belong to the PrintStream class.

To access them we need an instance of this class.

A static property called out of type PrintStream is created on the System class.

Hence to access the above methods we use the following statements:

System.out.println("foo");
System.out.print("foo");
查看更多
【Aperson】
6楼-- · 2019-01-05 11:01

System.out.println("...") in Java code is translated into JVM. Looking into the JVM gave me better understanding what is going on behind the hood.

From the book Programming form the Java Virtual Machine. This code is copied from https://github.com/ymasory/programming-for-the-jvm/blob/master/examples/HelloWorld.j.

This is the JVM source code.

.class public HelloWorld
.super java/lang/Object

.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
  getstatic java/lang/System/out Ljava/io/PrintStream;
  ldc "Hello, world"
  invokevirtual java/io/PrintStream/println
    (Ljava/lang/String;)V
  return

.end method
.end class

As "The JVM doesn't permit byte-level access to memory" the out object in type Ljava/io/PrintSteram; is stored in a stack with getstatic JVM command. Then the argument is pushed on the stack before called a method println of the java/io/PrintStream class from an instance named out. The method's parameter is (Ljava/lang/String;) and output type is void (V).

查看更多
闹够了就滚
7楼-- · 2019-01-05 11:02

System is a class in java.lang package. And out is a PrintStream object. Nice explanation @ http://lazy-geeks.blogspot.in/2015/01/what-is-systemoutprintln.html

查看更多
登录 后发表回答