๋ฐ˜์‘ํ˜•
์ž๋ฐ”๋Š” call by value (๊ฐ’์— ์˜ํ•œ ํ˜ธ์ถœ) ๋ฐฉ์‹์ด๋‹ค.

public class CallTest {

    public static void main(String[] args) {
        String test1 = "test1";
        String test2 = "test2";
        CallTest.callByValue(test1, test2); // ๊ฐ’์— ์˜ํ•œ ์ฐธ์กฐ
        
        // CallTest.callByValue ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ ํ›„ ๋ฐ”๊ฟง๋‹ค ํ•˜์—ฌ, test1, test2์— ์˜ํ–ฅ ์•ˆ๋ฏธ์นจ.
        System.out.println("test1:" + test1+", test2:"+test2); 
    }

    public static void callByValue(String test1, String test2) {
        String temp = test1;
        test1 = test2;
        test2  = temp;
        System.out.println("test1:" + test1+", test2:"+test2);
    }
}

/*
// result)
test1:test2, test2:test1
test1:test1, test2:test2
**/

ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ!
์ „๋‹ฌํ•œ ๊ฐ’ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ!
๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’๋“ค์€ ๊ฐ๊ฐ์˜ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜์— ์ €์žฅ๋จ. 
ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝํ•œ ์‚ฌํ•ญ์€ ํ˜ธ์ถœ์ž์˜ ์‹ค์ œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋ฐ˜์˜๋˜์ง€๋Š” ์•Š์Œ.

 

call by reference (์ฃผ์†Œ์— ์˜ํ•œ ํ˜ธ์ถœ)

๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋™์ผํ•œ ์œ„์น˜๋ฅผ ์ฐธ์กฐํ•˜๋ฏ€๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๋ณ€๊ฒฝํ•œ ์‚ฌํ•ญ์€ ์‹ค์ œ ํ˜ธ์ถœ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋ฐ˜์˜๋จ.
ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์‚ฌ์šฉ!
์ฐธ์กฐ์— ์˜ํ•œ ํ˜ธ์ถœ (C++ ํฌ์ธํ„ฐ)

/** 
C++ ์–ธ์–ด์—์„œ ๋ณ€์ˆ˜ ์ฃผ์†Œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ํ•˜์˜€์„ ๋•Œ
result) 
test1:test2, test2:test1
test1:test2, test2:test1 // ์ฃผ์†Œ ์ฐธ์กฐ์— ์˜ํ•ด ๊ฐ’์ด ๋ณ€๊ฒฝ ๋จ.
*/

 


์ฐธ๊ณ 

https://www.geeksforgeeks.org/difference-between-call-by-value-and-call-by-reference/

๋ฐ˜์‘ํ˜•

+ Recent posts