helloworld的运行机制

public class helloworld{

  public static void main(String[] args){

   String s;

    s = "hello world!";

    System.out.println("输出:"+s); 

}

}

一个类中有且仅有一个主类,主类必须是static,然后是public,并且主类没有返回值。

java文件首先编译器编译为字节码文件,有几个方法编译几个字节码文件;然后由虚拟机执行,但单线程程序中,主方法是虚拟机的入口,

程序在运行时,方法区存放:首先由编译器编译一个类的字节码文件,四个类分别是:helloworld.class,String[].class,String.class,System.class;

堆中存放对象,里面包含:3方法:System.out,System.in,System.error

栈中包含变量,有args 数组,但是null数组串,还有s;

当虚拟机运行时,访问数组地址s,由于s指向“Helloworld”,调用String类中tostring方法,再有system类中的system.out输出。

 

个人理解不是很清楚,欢迎大家指出,

posted on 2016-05-28 16:59  蜗牛的步伐  阅读(118)  评论(0编辑  收藏  举报

导航