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

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

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

namespace System {
  class out {
    static println ...         


        
相关标签:
19条回答
  • 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).

    0 讨论(0)
提交回复
热议问题