Java๋Š” pass by value?? ๐Ÿ™ƒ

๋…ธ์„ยท2021๋…„ 2์›” 20์ผ
2

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
3/3
post-custom-banner

๐Ÿ”” ์—ฌ๋Ÿฌ ์ž๋ฃŒ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ณ , ๊ฐœ์ธ ํ•™์Šต ์šฉ๋„๋กœ ์ •๋ฆฌํ•œ ํฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.
์ž˜๋ชป๋œ ์ ์ด ์žˆ๋‹ค๋ฉด ํ”ผ๋“œ๋ฐฑ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

๐Ÿ‘ป ๋ฐฐ๊ฒฝ์ง€์‹

pass by reference๋ž€?

Pass by reference
  • ์ฐธ์กฐ์˜ ๋ณต์‚ฌ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • ๋‹จ์ผ ์ฐธ์กฐ๋Š” ํ˜ธ์ถœํ•จ์ˆ˜์™€ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ ๋ชจ๋‘์—์„œ ๊ณต์œ ๋ฉ๋‹ˆ๋‹ค.
  • ์–ด๋–ค ์ฐธ์กฐ์˜ ๋ณ€ํ™”๋‚˜ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™”๋Š” caller scope์— ๋ฐ˜์˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • C++์—์„œ & ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ pass by reference๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค.
  • args์™€ parm๊ฐ€ ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๋™์ผํ•œ ๋ณ€์ˆ˜์ด๋‹ค.

Passing (Object references) By Value ๋ž€?

Passing Object references By Value
  • ์ฐธ์กฐ์˜ ๋ณ€๊ฒฝ์€ caller scope์— ๋ฐ˜์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค, ํ•˜์ง€๋งŒ ๊ทธ ๊ฐ์ฒด์˜ ๋ณ€ํ™”๋Š” ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.
  • ์™œ๋ƒํ•˜๋ฉด, ์ฐธ์กฐ๊ฐ€ ๋ณต์‚ฌ๋˜์—ˆ์ง€๋งŒ ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • argument์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
  • args์™€ parm๊ฐ€ ๋™์ผํ•˜์ง€ ์•Š๋‹ค.
  • call by value์™€ ๋™์˜์–ด
    • method ๋˜๋Š” argument์˜ ์‹œ์•ผ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋ถˆ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿ˜ฅ Java๊ฐ€ pass by reference๋ผ๊ณ  ์˜คํ•ด๋ฅผ ์‚ฌ๋Š” ์ด์œ .

๐Ÿ’จ1. Reference(์ฐธ์กฐ) ์šฉ์–ด์˜ ํ˜ผ๋™

  • ์ฐธ์กฐ ๋ผ๋Š” ์šฉ์–ด๋Š” ๊ฐœ๋ณ„์ ์ธ ์˜๋ฏธ๋กœ ์˜ค๋ฒ„๋กœ๋“œ ๋œ ๊ฒƒ์ด๋‹ค.

  • Java์—์„œ Reference ๋ž€ ๋‹จ์ˆœํžˆ ํฌ์ธํ„ฐ๋ฅผ ์˜๋ฏธ

    • C์–ธ์–ด์˜ ํ›„์†์ธ Java๋Š” ํฌ์ธํ„ฐ ๋ฅผ ์ฐธ์กฐ ๋กœ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•œ๋‹ค๋Š” ์ ์„ ์ฐจ์šฉํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค.
  • Java์˜ ์ฐธ์กฐํƒ€์ž…(Reference Type)
    - ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ๋ฐฐ์—ด, ์—ด๊ฑฐ, ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…

  • C++์˜ ์ฐธ์กฐ๋ณ€์ˆ˜(&)

  • Pass-by-reference ์˜ ๋งฅ๋ฝ์—์„œ๋Š” ์˜ค๋ฆฌ์ง€๋„ ๋ณ€์ˆ˜์˜ ํ•ธ๋“ค๋ง์„ ์˜๋ฏธ

  • ์ด๋Ÿฌํ•œ ์šฉ์–ด์˜ ํ˜ผ๋™์œผ๋กœ call by sharing ์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋„ ํ•จ.

    • call by value์™€ call by reference ์˜ ๊ฐ๊ฐ ํŠน์ง•์ด ์„ž์—ฌ์žˆ๋Š” ๊ฒƒ์„ ๋งํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    • args๋ฅผ parm์œผ๋กœ ๋ณต์‚ฌ๋Š” ํ•˜๋Š”๋ฐ, ์›๋ณธ ๊ฐ์ฒด์˜ ์ ‘๊ทผ์„ ๊ณต์œ ํ•ด์„œ ์›๋ณต ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ์ด๋Ÿฌํ•œ ์–ด์ค‘๊ฐ„ํ•œ? ํ˜ผํ•ฉ๋œ? Java์™€ ๊ฐ™์€ ์ผ€์ด์Šค๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ๊ธด ์šฉ์–ด ๊ฐ™์€๋ฐ, ์ž˜์“ฐ์ด์ง„ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

๐Ÿ’จ2. Java๋„ ์ฐธ์กฐ ํƒ€์ž…์€ call by reference "์ฒ˜๋Ÿผ" ๋™์ž‘

์ž๋ฐ”๋„ argument๋กœ reference type์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š”๋ฐ?์—์„œ ์‹œ์ž‘๋œ ํ˜ผ๋ž€ ๊ฐ™๋‹ค.

  • ์ „๋‹ฌ๋œ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

    • ์˜ˆ) setter ํ•จ์ˆ˜
  • ํ•˜์ง€๋งŒ, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ฐธ์กฐ๋ณ€์ˆ˜ a๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌ ํ•˜์—ฌ,

  • ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜ b ๋ผ๋Š” ๊ณต๊ฐ„์ด ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ ๊ฒƒ์ด๋‹ค.

    • img
  • ๋”ฐ๋ผ์„œ, ์ฐธ์กฐ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์„ ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ํ•˜์ง€๋งŒ ์ฐธ์กฐ๋ณ€์ˆ˜ b ์™€ ์ฐธ์กฐ๋ณ€์ˆ˜ a๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๊ฐœ๋ณ„์ ์ธ ์š”์†Œ์ด๋‹ค.

  • ์ฆ‰, ์ „๋‹ฌ๋œ ์ฐธ์กฐ๋ณ€์ˆ˜ a ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค.

    • ์˜ˆ๋ฅผ๋“ค์–ด, new ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด b์— ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•œ๋‹ค๋ฉด?
    • b์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„, a๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ƒํƒœ๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

๊ฒฐ๋ก 

  • Java๋Š” C์–ธ์–ด์ฒ˜๋Ÿผ ์ฃผ์†Œ๊ฐ’์„ ๋‹ค๋ฃฐ ์ˆ˜ ์—†๋‹ค.
  • ๋ณ„๊ฑฐ ์•„๋‹Œ๋Œ€ ๊ฐ๊ฐ์˜ ์–ธ์–ด์—์„œ ๋งํ•˜๋Š” ์ฐธ์กฐ(Reference)์˜ ์ •์˜๊ฐ€ ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ผ์„œ ์ƒ๊ธด ํ˜ผ๋ž€์ด๋‹ค.
  • ์ž๋ฐ”๋Š” Passing object references by value

Pyro๋‹˜ ์ž๋ฃŒ ์Šคํฌ๋žจ
์ž๋ฐ”์—์„œ pass by value ๋งŒ์„ ํ—ˆ์šฉํ•˜๊ณ ,
call by reference ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์†Œ๋ฆฌ๋Š”,
function call ๋กœ ์ธํ•œ side effect ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์–ธ์–ด ์„ค๊ณ„ ์ฒ ํ•™์„ ์ด์•ผ๊ธฐํ•œ ๊ฒƒ์ด๋‹ค.
๊ทธ๋ƒฅ swap ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ์ „๋ถ€ call by value ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜์ž.


๋ถ€๋ก

C++ ์˜ˆ์ œ

#include <iostream>

using namespace std;

void foo(int &x) { //์ฐธ์กฐ ์—ฐ์‚ฐ์ž
    x *= 10;
}

void bar(int *x) {
    *x *= 10;
}

int main(int argc, char const *argv[])
{
    int x = 10;
    int y = 10;
    foo(x); //call by reference
    bar(&y); //call by addresss
    cout << x << endl;
    cout << y << endl;

    return 0;
}
output

> 100
> 100

Java ํ–‡๊ฐˆ๋ฆฌ๋Š” ์˜ˆ์ œ

class Company{
    private String name;
    
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

public class copyEx2{
    public static void main(String[] args){
	Company com1 = new Company();
        com1.setName("Google");
        show(com1);
        System.out.println("main(): "+com1.getName());
    }
    public static void show(Company com){
        com = new Comapny(); // ๐Ÿ” ์ฃผ๋ชฉ!
        com.setName("Yahoo");
        System.out.println("show(): "+com.getName());
    }
}
output

> show():yahoo
> main():Google
  • ๐Ÿ’ฅ ์ž๋ฐ”๊ฐ€ ์ •๋ง๋กœ call by reference ์˜€๋‹ค๋ฉด main():yahoo ์ด์–ด์•ผ ํ•œ๋‹ค.


์ฐธ๊ณ 

profile
์นด๋ฅดํŽ˜๋””์— 
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 2์›” 22์ผ

์šฐ์™€๐Ÿ‘๐Ÿ‘ ๊ทธ๋ฆผ์ด ์žˆ์œผ๋‹ˆ๊นŒ ํ›จ์”ฌ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜๋„ค์š”๐Ÿ˜ฏ๐Ÿ˜ฏ

1๊ฐœ์˜ ๋‹ต๊ธ€