[JAVA] 7. JVM Ⅰ. 사전 지식

최재원's avatar
Feb 08, 2025
[JAVA] 7. JVM Ⅰ. 사전 지식
변수(int, double, boolean, String, char)
static(main이 실행되기 전 메모리에 로드된다.) 공간 분리(class별로 분리됨)
static은 정적이고 한 개만 메모리에 로드된다.(유일하다)
메서드(함수) - 4가지 유형(기본, 인수 1개, 인수 여러개, 리턴이 있는) - void(리턴이 없는)

1. heap

main이 실행 중에 사용하는 공간
new가 되면, static이 붙지 않은 모든 데이터(메서드, 변수)가 heap에 로드된다.
class Student { int age; String name; int no; void show() { System.out.println(age); System.out.println(name); System.out.println(no); System.out.println(); } } public class Heap01 { public static void main(String[] args) { Student s1 = new Student(); // new가 되면, static이 붙지 않은 모든 데이터(메서드, 변수)가 heap에 로드된다. s1.no = 1; s1.name = "홍길동"; s1.age = 10; s1.show(); Student s2 = new Student(); s2.no = 2; s2.name = "이도"; s2.age = 15; s2.show(); } }
  1. static으로 작성된 코드 static메모리에 로드
  1. static으로 작성된 main메서드 실행
  1. new로 사용된 Student클래스 heap에 로드
  1. heap에 저장된 Student클래스를 s1이름으로 사용
Student 클래스에서 show 메서드만 static으로 로드한 경우
class Student { int age; String name; int no; static void show() { System.out.println(age); System.out.println(name); System.out.println(no); System.out.println(); } } public class Heap01 { public static void main(String[] args) { Student s1 = new Student(); // new가 되면, static이 붙지 않은 모든 데이터(메서드, 변수)가 heap에 로드된다. s1.no = 1; s1.name = "홍길동"; s1.age = 10; Student.show(); } }
notion image
정적인 맥락에서 비정적 변수 연령을 참조할 수 없습니다
show 메서드는 age, name, no값을 찾을 수 없다. 이 값들은 heap에 저장되어 있기 때문이다.
static이나 show 메서드의 스택에 존재한다면 찾을 수 있다.

2. stack

메서드가 실행될 때 사용하는 저장 공간
메서드 내부의 변수, 클래스 이름이 저장된다.
package jvm; public class Stack01 { static void m1() { int a = 1; m2(); } static void m2() { int b = 1; } public static void main(String[] args) { System.out.println("13번라인"); m1(); System.out.println("16번라인"); } }
package jvm; public class Stack02 { static void m1() { int a = 1; m1(); // 재귀함수, 계속 반복된다. 특정 조건에 종료되게 만들어야 한다. // 오류는 위에서 아래로 확인 } public static void main(String[] args) { m1(); } }

3. queue

메서드의 실행 순서를 저장하는 공간
package jvm; public class Queue01 { static void m1() { int n1 = 1; System.out.println("🚒"); System.out.println("🏍️"); System.out.println("🚲️"); m2(); System.out.println("🚅️"); System.out.println("✈️"); } static void m2() { int n2 = 2; System.out.println("🍕"); System.out.println("🍔"); System.out.println("🌭"); System.out.println("🥚"); System.out.println("🧀"); } public static void main(String[] args) { System.out.println("main 시작"); m1(); System.out.println("main 종료"); } }
notion image

4. 참조 자료형

notion image
기본 자료형: 값이 있다
참조 자료형: 주소가 있다. 두번 찾아가야한다. 1)주소 → 2)실제 값
참조 자료형의 크기는 기본적으로 4Byte다.
package jvm; class Data { int num; } public class Ref01 { public static void main(String[] args) { Data d1 = new Data(); Data d2 = d1; d1.num = 10; d2.num = 20; System.out.println("d1.num = " + d1.num); System.out.println("d2.num = " + d2.num); } }
notion image
d1과 d2는 같은 곳을 가리키고 있다. heap에 저장된 data 클래스의 주소를 저장하고 있기 때문
notion image
Share article

jjack1