为什么方法执行完就出栈?main方法会吗?

解决方案 »

  1.   

    java方法的调用是在方法栈中运行,分为入栈、出栈、压栈、弹栈、执行代码五个部分。
    main方法同样会
      

  2.   

    加入main调用方法A,A调用B。那么栈内的元素应该是main A BB执行完了就要出栈,此时栈顶A开始执行;A执行完了就要出栈,此时栈顶main开始执行;main执行完了,栈空了,程序就结束了。
      

  3.   

    编译器原理摆在那里 不解释不出栈那还留着?当然是不管什么方法用完就OVER了啊
      

  4.   

    The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method. 如果不出栈就没法丢弃刚执行完的方法信息,也正好顺理成章执行栈顶元素(即invoking method的接下来内容)
      

  5.   

    进入一个方法A,就是压了一次栈,对应的,执行完一次方法,自然是出栈,继续回到外层方法B的后续代码,也就是前面调用A的代码的后面,继续执行。
      

  6.   

    这个图没看太懂....但是知道了这是JAVA编程运行思想基础规则