Java method를 정의하고 사용할 때 Argument와 Parameter는 무조건 사용한다.
이때, 두 개의 차이점이 많이 헷갈릴 수 있다.
차이점에 대해 공부하고자 한다.
일단 오라클 공식 문서에 있는 Parameter와 Argument의 정의이다.
Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.
( Paramters는 메서드 선언할 때 있는 변수를 나타낸다.
Arguments는 메서드를 호출할 때 보내는 값이다.
Arguments는 매개변수와 데이터 타입과 순서가 일치해야 한다. )
Parameter
parameter는 한글로 매개변수라고 불린다. 매개변수 즉, 어떤 값을 중매? 해주는 느낌이다.
다시 말해 parameter는 함수 혹은 메서드를 정의에서 나열되는 변수 이름이다.
static int plus(int a, int b){
int sum = a+b;
return sum;
}
위 코드처럼 int a, int b는 Paramter(매개변수)라고 불린다. 즉, plus method를 정의할 때 쓰는 2개의 변수가 a, b이다.
메서드를 선언할 때 정의하는 변수를 매개변수.
static String changeToLargeText(String userName)
여기서 Parameter 매개변수는 userName이다.
Argument
메서드에 호출할 때 메서드에게 보내는 값을 Argument라고 한다. 인자값 인수값으로도 불린다.
public static void main(String[] args) {
int plusNumber = plus(5,5);
System.out.println("plusNumber = " + plusNumber);;
}

주의사항은 Java에서는 메서드를 정의할 때 Parameter의 변수 타입과 개수가 메서드를 호출할 때 Argument의 개수와 타입이 무조건 동일해야 한다.
개수가 다를 시 에러가 난다.

에러를 읽어보면 argument lists의 길이다 달라 에러가 났다. 필요한 것은 int, int 2가지인데 인자값으로 int하나만 보내 에러가 났다.
결론
Parameter (인자(인수)) 값은 메서드를 호출할 때 전달하는 값.
Argument (매개변수)는 메서드를 정의할 때 쓰는 변수.
'Java' 카테고리의 다른 글
[Java] Method Signature (0) | 2024.12.01 |
---|---|
[Java] Stack Trace (0) | 2024.11.18 |
[Java] 배열을 쉽게 출력하기! (1) | 2024.09.16 |
[Java의 정석] Chapter4 연습문제 (3) | 2024.09.13 |
[Java14] 새로운 Switch문 (0) | 2024.09.13 |