song.log

[자바 기술면접] 08. 값에 의한 호출 vs 참조에 의한 호출 본문

StudyLog/Java interview

[자바 기술면접] 08. 값에 의한 호출 vs 참조에 의한 호출

SingaKorean 2023. 4. 28. 17:24
반응형

- 정의

값에 의한 호출(Call by Value): 메서드에 인자로 전달되는 값은 복사되어 전달됩니다. 즉, 인자로 전달된 변수의 값이 복사된 후 전달되므로, 메서드에서 인자 값을 변경하더라도 호출한 쪽의 변수 값은 변경되지 않습니다.

 

참조에 의한 호출(Call by Reference): 메서드에 인자로 전달되는 것이 변수의 주소(참조)이므로, 인자로 전달된 변수를 메서드에서 직접 참조하여 값을 변경하면 호출한 쪽의 변수 값도 함께 변경됩니다.

 

- 영어 정리 : 

An argument can be passed in two ways. They are passing by value and passing by reference.

Passing by value: This method copies the value of an argument into the formal parameter of the subroutine.

Passing by reference: In this method, a reference to an argument is passed to the parameter.

 

In Java, there are two ways that an argument can be passed to a subroutine: pass-by-value and pass-by-reference.

 

Pass-by-value: In pass-by-value, a copy of the value of the argument is passed to the subroutine. Any changes made to the parameter within the subroutine have no effect on the original argument. This is because the copy is separate from the original value. Primitive data types, such as int, float, and boolean, are passed by value.

 

Pass-by-reference: In pass-by-reference, a reference to the argument is passed to the subroutine. This means that any changes made to the parameter within the subroutine will also affect the original argument. Objects and arrays are passed by reference. When an object or array is passed by reference, the reference to the object is passed to the method, not the object itself.

 

It's important to note that even though objects and arrays are passed by reference, the reference itself is passed by value. This means that if a method changes the reference to point to a different object, the change will only affect the method and not the caller.

반응형
Comments