Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- python
- control
- Algorithm
- Rag
- DATAPATH
- data structure
- Pipelining
- openai
- CSS
- mysql
- architecture
- react
- Linux
- for
- system
- javascript
- github
- XML
- html
- MIPS
- Java
- web
- Class
- computer
- function
- php
- DS
- instruction
- AI
- DB
Archives
- Today
- Total
YYYEJI
[JAVA] Pass by Value와 Pass by Reference의 차이점 본문
728x90

Pass by Value는
복사된 값이 전달되는 방식으로, 원본 값에 영향을 주지 않습니다.
즉 들어온 caller와 callee의 파라미터는 서로 다른 변수입니다.
아래는 Pass by Value에 관련된 예제입니다.
public static void main(String[] args) {
int a = 1;
System.out.println("a before function call: " + a);
int result = sum(a, a);
System.out.println("The result is " + result + ".");
System.out.println("a after function call: " + a);
}
private static int sum(int a, int b) {
return a+b;
}

Pass by Reference
주소값을 전달하는 방식으로, 원본 값에 영향을 줍니다.
Caller와 callee 파라미터의 변수가 동일하기 때문에 method에서 값을 수정하면 본래 변수에도 동일하게 적용이 됩니다.
C에서는 두 개념 모두 존재하지만 자바에서는
Pass by Value로만 동작합니다.
◡̈
728x90
'Java' 카테고리의 다른 글
| [JAVA] Class Type인 변수 할당하기 (0) | 2022.12.26 |
|---|---|
| [JAVA] Setter/Getter 메소드 (0) | 2022.12.26 |
| [JAVA] Format parameter와 Actual parameter 이해하기 (0) | 2022.12.26 |
| [JAVA] This와 Shadowed field 이해하기 (0) | 2022.12.26 |
| [JAVA] Main method 이해하기 (0) | 2022.12.26 |