Call by Value
(값에 의한 호출)Java
의 경우, 항상 call by value
로 값을 전달한다// Call by Value
public class Cup {
static void pourCoffee(int cup, int americano) {
cup += americano;
}
public static void main(String[] args) {
int cup = 0;
int americano = 10;
System.out.println("컵의 양: "+ cup + "리터");
// 컵에 아메리카노 채우기
pourCoffee(cup, americano);
// 이후 컵에 담긴 아메리카노의 양
System.out.println("call by value 컵의 양: "+ cup + "리터");
}
}
// 결과
// 컵의 양 : 0리터
// call by value 컵의 양 : 0리터
Call by Reference
(참조에 의한 호출)참조 자료형 : call by reference로 동작 (Array, Class instance)
함수 호출 시 인자로 전달되는 변수의 레퍼런스를 전달한다
// Call by Reference
public class Cup {
int cup = 0;
Cup(int cup){
this.cup = cup;
}
static void pourCoffee(Cup cup, int americano) {
cup.cup += americano;
}
public static void main(String[] args) {
int americano = 10;
Cup cup = new Cup(0);
System.out.println("컵의 양: "+ cup.cup + "리터");
System.out.println("컵의 양: "+ cup + "리터"); // 객체주소가 참조되어진 것을 확인할 수 있다
// 컵에 아메리카노 채우기
pourCoffee(cup, americano);
// 이후 컵에 담긴 아메리카노의 양
System.out.println("call by reference 컵의 양: "+ cup.cup + "리터");
System.out.println("call by reference 컵의 양: "+ cup + "리터"); // 객체주소가 참조되어진 것을 확인할 수 있다
}
}
// 결과
// 컵의 양 : 0리터
// call bt reference 컵의 양 : 10리터
장점
단점
Call by Value
원래의 값이 보존
Call by Reference
원래의 값이 영향을 받음
https://dreamcoding.tistory.com/10
package java_test;
// Call by Value
//public class Cup {
//
// static void pourCoffee(int cup, int americano) {
// cup += americano;
// }
//
// public static void main(String[] args) {
// int cup = 0;
// int americano = 10;
//
// System.out.println("컵의 양: "+ cup + "리터");
//
// // 컵에 아메리카노 채우기
// pourCoffee(cup, americano);
//
// // 이후 컵에 담긴 아메리카노의 양
// System.out.println("call by value 컵의 양: "+ cup + "리터");
// }
//
//}
/*
* 컵에 아메리카노를 담는다는 상황을 가정한 코드이다
* 커피를 붓는 메소드인 pourCoffee메소드를 사용하면 컵의 양이 10리터 만큼 늘어난 양이 나와야 한다
* 그런데 커피를 부었는데 여전히 컵의 양은 0이다
* 컵과 아메리카노 양에 대한 값만 복사해서 가져온 것이라 실질적인 컵과 아메리카노의 양에는 영향을 끼치지 못한
*/
// Call by Reference
public class Cup {
int cup = 0;
Cup(int cup){
this.cup = cup;
}
static void pourCoffee(Cup cup, int americano) {
cup.cup += americano;
}
public static void main(String[] args) {
int americano = 10;
Cup cup = new Cup(0);
System.out.println("컵의 양: "+ cup.cup + "리터");
// 컵에 아메리카노 채우기
pourCoffee(cup, americano);
// 이후 컵에 담긴 아메리카노의 양
System.out.println("call by reference 컵의 양: "+ cup.cup + "리터");
}
}
/*
* 생성자로 컵을 생성하여 컵을 객체화해서 컵을 생성시 하나의 주소값을 가지고 있도록 했다
* 이후 객체화된 컵에 앙메리카노를 부으면 객체화되어 주소값을 가진 컵 객체의 주소값을 참조하여
* 아메리카노를 붓기 때문에 나중에 확인하면 참조한 컵에 부었기 때문에 값이 추가되어 나타난다
*/
참고 자료 :
Java 의 Call by Value, Call by Reference
[Java] 함수 호출 'CallByValue' 와 'CallByReference' 의 차이
Call by value vs Call by Reference