CallByvalueNReference

 

** 수정했음***

메소드 호출과 관련된 개념으로
매개변수에 자료형값을 넣을 수도 있고(Call by Value) 
매개변수에 메모리의 주소값을 전달(복사)하는 경우 즉 배열을 넣을 수도 있다.(Call by Reference)

 

 

* 매개변수의 타입이 기본자료형 (primative type) 인 경우:  Call By value

 

====여긴 class안 임!======

static void callByValue (int first,int second) {

int temp = first;

first = second;

second = temp;

System.out.printf("callByValue메소드 안] first:%d,second:%d%n",first,second);

}////call

 

 

============= 여긴 main 안 =======

int first = 1, second = 10;

System.out.printf("main메소드 안-callByRefernce 호출전]first:%d,second:%d%n",first,second);

callByValue(first, second);                     ---> 호출

System.out.printf("main메소드 안-callByValue 호출후]first:%d,second:%d%n",first,second);

 

 

 

매개변수 타입이 참조형인 경우  : call by reference

 

 값이 아니라 주소값이 매개변수에 전달이 됨 그니까 배열이 매개변수에 들어가는거!!

 같은 메모리를 참조한다.

 여러개 값 반환,전달 가능하고 void도 반환(return) 이 가능한 놀라운효과

 

========class 안===========================

static void callByRefernce(int [ ] arr) {                                     배열이 직접 들어가 있네

int temp = arr[0];

arr[0] = arr[1];

arr[1] = temp;

System.out.printf("callByValue메소드 안]arr[0]:%d,arr[1]:%d%n",arr[0],arr[1]);

}///call

 

=========main 안===========================

        int [] ref = new int[2];

        ref[0] =1;

        ref[1] =10;

        System.out.printf("main메소드 안-callByRefernce 호출전]ref[0]:%d,ref[1]:%d%n",ref[0],ref[1]);

        callByRefernce(ref);                                   -> 호출! ()안에 배열명이 들어가 있네

        System.out.printf("main메소드 안-callByRefernce 호출후]ref[0]:%d,ref[1]:%d%n",ref[0],ref[1]);

 

 

 

'학원 > JAVA' 카테고리의 다른 글

9.23 8-1일 차 (가위바위보게임 Methover.)  (0) 2022.09.24
9.22 7-6일 차 CallByRefExample  (1) 2022.09.23
9.22 7-4일 차 MethodShape04  (0) 2022.09.22
09.22 7-3일 차 MethodShape03  (1) 2022.09.22
9.22 7-2일 차 MethodShape02  (0) 2022.09.22

+ Recent posts