JAVA/java 이론
[자바JAVA]JVM의 메모리구조- 호출스택
자바칩 프라푸치노
2020. 10. 8. 15:49
JVM의 메모리 구조에서 호출스택에 대해 알아보겠습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package sec02_exam;
public class CallStackEx {
public static void main(String[] args) {
System.out.println("main()실행됨");
CallStackEx.firstMethod();
System.out.println("main()종료됨");
}
public static void firstMethod() {
System.out.println("firstMethod()실행됨");
CallStackEx.secondMethod();
System.out.println("firstMethod()종료됨");
return;
}
public static void secondMethod() {
System.out.println("secondMethod()실행됨");
System.out.println("secondMethod()종료됨");
}
}
|
cs |
<실행결과>
위의 코드에서 실행 결과는 이렇게 됩니다.
여기서 CallStack의 실행프로세스는 이렇게 됩니다.
main을 실행하던 중 메인에서 firstMethod를 호출했으니 firstMethod를 실행하다가
firstMethod에서 secondMethod를 호출해서 secondMethod를 실행하고 끝나면 다시 대기중이던
firstMethod를 실행하고 끝나면 main에서 나머지 실행을 하고 끝나는 것이죠.
이 실행은 지금은 간단하지만 나중에 코드가 복잡해지고
메서드가 이리갔다 저리갔다 하면 이 구조를 잘 알아놓는 것이 코드를 이해하기에 좋습니다.
728x90