Java main() Method 호출 이해
가변 파라미터 vs 배열 파라미터
가변 파라미터
static void hello(String... names) {
    for (int i = 0; i < names.length; i++) {
      System.out.printf("%s님 반갑습니다.\n", names[i]);
    }
  }
기본호출 
hello("홍길동", "임꺽정", "유관순");
배열에 담아서 호출
String[] arr = {"김구", "안중근", "윤봉길", "유관순"};
    hello(arr);
배열 파라미터
static void hello2(String[] names) {
    for (int i = 0; i < names.length; i++) {
      System.out.printf("%s님 반갑습니다.\n", names[i]);
    }
  }
배열에 담아서 전달/기본 파라미터호출법 x
String[] arr2 = {"김구", "안중근", "윤봉길", "유관순"};
hello2(arr2);
call by value
package com.eomcs.lang.ex07;
public class Exam0310 {
  static void swap(int a, int b) {
    System.out.printf("swap(): a=%d, b=%d\n", a, b);
    int temp = a;
    a = b;
    b = temp;
    System.out.printf("swap(): a=%d, b=%d\n", a, b);
  }
  public static void main(String[] args) {
    int a = 100;
    int b = 200;
    // swap() 호출할 때 a 변수의 값과 b 변수의 값을 넘긴다.
    // => 그래서 "call by value"라 부른다.
    // => 비록 swap()에서 a와 b라는 이름의 변수가 있지만,
    //    이 변수는 main()에 있는 변수와 다른 변수이다.
    swap(a, b);
    System.out.printf("main(): a=%d, b=%d\n", a, b);
  }
}
JVM 메모리 실행순서
- Method Area영역에 class Exam0310 실행
 - Stack 영역에 main() 호출 변수 값 준비
 - Stack 영역에 swap() 호출 변수 값 준비
 - swap() 실행 완료 메모리 삭제
 - main() 실행 완료 메모리 삭제
 - JVM 종료 os에서 메모리 회수
 
main() 실행
- 메서드를 호출하면 메서드가 사용할 로컬변수가 스택 메모리 영역에 준비된다
 
JVM 메모리 용어
Stack
- 호출 메서드의 로컬 변수가 놓인다
 
Heap
new명령으로 만든 인스턴스 변수가 놓인다
Method Area
- 실행할 클래스 코드가 놓인다
 
call by reference
package com.eomcs.lang.ex07;
public class Exam0320 {
  static void swap(int[] arr) {
    System.out.printf("swap(): arr[0]=%d, arr[1]=%d\n", arr[0], arr[1]);
    int temp = arr[0];
    arr[0] = arr[1];
    arr[1] = temp;
    System.out.printf("swap(): arr[0]=%d, arr[1]=%d\n", arr[0], arr[1]);
  }
  public static void main(String[] args) {
    int[] arr = new int[] {100, 200};
    swap(arr); // 배열 인스턴스(메모리)를 넘기는 것이 아니다. 
    // 주소를 넘기는 것이다.
    // 그래서 "call by reference" 라 부른다.
    System.out.printf("main(): arr[0]=%d, arr[1]=%d\n", arr[0], arr[1]);
  }
}
JVM에 기본으로 설정되어 있는 프로퍼티를 모두 출력
package com.eomcs.lang.ex07;
public class Exam0630 {
  public static void main(String[] 변수명은상관없다) {
    java.util.Properties props = System.getProperties();
    java.util.Set keySet = props.keySet();
    for (Object key : keySet) {
      String value = props.getProperty((String) key);
      System.out.printf("%s ==> %s\n", key, value);
    }
  }
}
      
    
Leave a comment