hello world implementation of the principle of

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args)
 . 3      {    
 . 4              String S;
 . 5                S = "the Hello World!";   // definition string S 
. 6            System.out.println (S); 
 . 7      }
 . 8 }
↑↑↑ is a simple java program HelloWorld 
Java code is compiled Java source code is accomplished by a compiler, the flowchart as shown below:

Performs Java bytecode is done by the JVM execution engine, the flowchart as shown below:

Program execution is as follows: 

① using javac to compile the files to HelloWorld.java HelloWorld.class bytecode file. (Jdk javac compiler development toolkit contains tools and runtime environment jre)

 ② class loading phase, loaded into the JVM's memory, the Main program is to the entrance, and then interpreted by the JVM to execute the virtual machine, the virtual machine to achieve some, will be utilized by the JVM bytecode interpreter particular machine system code execution, thereby improving efficiency.

  responsible for java language interpreted bytecode file is java virtual machine, both JVM (java Virtual Machine). (JVM virtual machine is running java bytecode file)

 ③  虚拟机通过类找到HelloWorld的主方法(程序的入口方法),访问权限为public(公有可用),虚拟机传递String[](字符串数组对象:空数组)类型参数的地址到主方法的args中去,并在栈区为args开辟内存空间,返回一个void的返回值;

  JVM 将内存区域划分为 Method Area(Non-Heap)(方法区) ,Heap(堆) , Program Counter Register(程序计数器) ,   VM Stack(虚拟机栈,也有翻译成JAVA 方法栈的),Native Method Stack  ( 本地方法栈 ),其中Method Area 和  Heap 是线程共享的  ,VM Stack,Native Method Stack  和Program Counter Register  是非线程共享的。

 JVM 内存中线程是否共享示意图:

④  定义一个String(标准类库中的)类型的变量(在栈区开辟空间)s,s的值不确定(垃圾值,编译无法通过);

 ⑤  s = “Hello World!”,对象“Hello World!”在方法区的常量数据区开辟空间,属性即为:Hello World!,方法即为:toString(),变量s存放对象“Hello World!”的地址;

 ⑥  虚拟机找到标准类库中的System.class类并加载到内存中(即方法区的类代码区中),System.out为标准字节输出流对象(),并调用println()方法将变量s的值打印到屏幕上。

 

PS: 虚拟机调用主方法时会创建三个默认对象:System.out(标准字节输出流对象)、System.in(标准字节输入流对象)和System.error(标准字节出错流对象).

以上共涉及:

1个java文件:HelloWorld.java

4个class类: HelloWorld.class、String[].class、String.class、System.class

5个对象: “Hello World!”、String[]、System.out、System.in、System.error

2个变量:args、s

3个方法:main()、toString()、println()

Guess you like

Origin www.cnblogs.com/JMrLi/p/11139498.html