반응형

# 메서드의 매개변수 전달기법(parameter passing)

## 값 전달 기법 (Call By Value)

  • 기억공간 개별, 값 전달.
int a = 10; int b = 20;
int v = sum(a, b);	// method 호출 부, 여기서 a, b는 값(value), 실 인수

public int sum(int a, int b) {	// method 정의 부, 여기서 a, b는 가 인수
	int v = a + b;
    return v;
}

## 번지전달 기법 (Call By Reference)

  • 기억공간 공유, 번지 전달.
int[] arr = {10, 20, 30};

int v = sum(arr);	// method 호출 부, arr은 번지(reference)

public int sum(int[] a) {	// method 정의 부
	int v = 0;
    for (int i = 0; i < a.length; i++) {
    	v += a[i];
    }
    return v;
}

## 매개변수 전달기법(실습)

public class TPC07 {

	public static void main(String[] args) {
		// 매개변수 전달 관련
		int a = 20;
		float b = 56.7f;
		
		// a + b 계산.
		float v = sum(a, b);	// 값 전달 기법(Call By value)
		System.out.println("sum : " + v);
		
		
		int[] arr = {1, 2, 3, 4};	
		
		int vv = arrSum(arr);	// 번지 전달 기법(Call By Reference)
		System.out.println("arrSum : " + vv);
	}
	
	public static float sum(int a, float b) {
		float v = a + b;
		return v;
	}
	
	public static int arrSum(int[] arr) {
		int sum = 0;
		
		for(int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		
		return sum;
	}
}

 

# JVM의 메모리 모델

## JVM Memory Model 1

  • JVM은 가상머신(프로세스)이므로 동작 시 4종류(method Area, stack Area, heap Area, literal Pool)를 이용 프로그램을 동작시킨다.
  • JVM이 class(실행 클래스)를 실행하는 절차는 아래와 같다.
1. 해당 클래스를 현재 디렉토리에서 찾는다. (찾기)

2. 찾은 클래스 내부에 있는 "static 키워드"가 있는 메서드를 메모리로 로딩한다. (로딩)
- method Area의 static zone에 로딩 한다. main(), add() method

3. static zone에서 main() 메서드를 실행한다. (호출, 시작)
- main() method가 호출되면 main() method의 호출정보가 Stack Area에 들어간다. (Push)
- 프로그램이 시작되는 부분이다.

4. Stack Area가 비어 있으면 프로그램이 종료된 것이다.

출처 : 인프런 박매일 Java TPC

  • method Area : Method의 byte code가 저장되는 영역 (static zone, non-static zone 으로 나뉜다.)
  • stack Area : 메서드가 호출되면 메서드의 호출정보가 저장되는 영역 (= Call Stack Frame Area)
  • heap Area : 객체가 생성되는 영역(new 연산자)
  • literal Pool : 문자열(객체)상수가 저장되는 영역

## JVM 메모리 모델1(실습)

public class TPC08 {

	public static void main(String[] args) {
		int a = 30;
		int b = 50;
		
		int v = add(a, b);	// static method call.
		
		System.out.println("sum : " + v);
	}
	
	public static int add(int a, int b) {
		int sum = a + b;
		return sum;
	}

}

## JVM Memory Model 2

  • JVM이 class(실행 클래스)를 실행하는 절차는 아래와 같다.
1. 해당 클래스를 현재 디렉토리에서 찾는다. (찾기)

2. 찾은 클래스 내부에 있는 "static 키워드"가 있는 메서드를 메모리로 로딩한다. (로딩)
- method Area의 static zone에 로딩 한다. main() method

3. static zone에서 main() 메서드를 실행한다. (호출, 시작)
- main() method가 호출되면 main() method의 호출정보가 Stack Area에 들어간다. (Push)
- 프로그램이 시작되는 부분이다.

4. Stack Area가 비어 있으면 프로그램이 종료된 것이다.

출처 : 인프런 박매일 Java TPC

  • method Area : Method의 byte code가 저장되는 영역 (static zone, non-static zone 으로 나뉜다.)
  • stack Area : 메서드가 호출되면 메서드의 호출정보가 저장되는 영역 (= Call Stack Frame Area)
  • heap Area : 객체가 생성되는 영역(new 연산자)
  • literal Pool : 문자열(객체)상수가 저장되는 영역

## JVM 메모리 모델2(실습)

public class TPC09 {

	public static void main(String[] args) {
		int a = 56;
		int b = 60;
		
		// a + b = ?
		TPC09 tpc = new TPC09();	// heap Area(힙 영역)에 객체 생성.
		int sum = tpc.sum(a, b);
		System.out.println("sum : " + sum);
	}

	public int sum(int a, int b) {
		int sum = a + b;
		return sum;
	}
}
반응형

+ Recent posts