日期:2014-05-20  浏览次数:20734 次

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

------解决方案--------------------
java方法的调用是在方法栈中运行,分为入栈、出栈、压栈、弹栈、执行代码五个部分。
main方法同样会
------解决方案--------------------
加入main调用方法A,A调用B。那么栈内的元素应该是main A B

B执行完了就要出栈,此时栈顶A开始执行;

A执行完了就要出栈,此时栈顶main开始执行;

main执行完了,栈空了,程序就结束了。
------解决方案--------------------
拉完屎你还蹲在茅坑干嘛
------解决方案--------------------
栈式执行,这个是基本那。
方法里的变量都是在栈中自动分配,自动释放。
方法执行前,根据变量的多少,移动一下栈指针,
方法执行后,栈指针再移回去。
------解决方案--------------------
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的接下来内容)
------解决方案--------------------
进入一个方法A,就是压了一次栈,对应的,执行完一次方法,自然是出栈,继续回到外层方法B的后续代码,也就是前面调用A的代码的后面,继续执行。