๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[์šฉ์–ด][Java] Pass by Value, Pass by Reference + ์ž๋ฐ”์—์„œ์˜ ์‚ฌ์šฉ

์ถœ์ฒ˜

ChatGPT


Pass by Value

์ •์˜

Pass by Value๋ž€ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ์— ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ๋•Œ, ์ธ์ž์˜ ์‹ค์ œ ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ์ „๋‹ฌ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ฆ‰, ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ธ์ž์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ํ•จ์ˆ˜๋‚ด์—์„œ๋Š” ์ด ๋ณต์‚ฌ๋œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

ํŠน์ง•

๋…๋ฆฝ์„ฑ

ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ณ€์ˆ˜์˜ ๋ณ€๊ฒฝ์ด ํ˜ธ์ถœ์ž์˜ ๋ณ€์ˆ˜์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.

์•ˆ์ „์„ฑ

์™ธ๋ถ€ ๋ณ€์ˆ˜์˜ ์ƒํƒœ๋ฅผ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ณต์‚ฌ ๋น„์šฉ

ํฐ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ์™€ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์‹œ

public class PassByValueExample {
    public static void main(String[] args) {
        int number = 10;
        modifyValue(number);
        System.out.println("Original number: " + number); // 10
    }

    public static void modifyValue(int num) {
        num = 20; // Only modifies the local copy
    }
}

 

์„ค๋ช…

 

  • modifyValue ๋ฉ”์„œ๋“œ์— number๊ฐ€ ์ „๋‹ฌ๋  ๋•Œ, number์˜ ๊ฐ’์ธ 10์ด ๋ณต์‚ฌ๋˜์–ด num ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ๋‹ค.
  • ํ•จ์ˆ˜ ๋‚ด์—์„œ num์„ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„ ์›๋ž˜์˜ number๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

 

Pass by Reference

์ •์˜

Pass by Reference๋ž€ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ์— ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ๋•Œ, ์ธ์ž์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ํ•จ์ˆ˜๋Š” ์›๋ž˜ ๋ฐ์ดํ„ฐ์— ์ง์ ‘ ์ ‘๊ทผํ•ด ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

ํŠน์ง•

๊ณต์œ ์„ฑ

ํ•จ์ˆ˜ ๋‚ด์—์„œ์˜ ๋ณ€๊ฒฝ์ด ํ˜ธ์ถœ์ž์˜ ๋ณ€์ˆ˜์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

ํšจ์œจ์„ฑ

ํฐ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ํšจ์œจ์ ์ด๋‹ค

์œ„ํ—˜์„ฑ

์™ธ๋ถ€ ๋ฐ์ดํ„ฐ์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

์˜ˆ์‹œ(๋‹ค๋ฅธ ์–ธ์–ด(C++)์—์„œ์˜ ์˜ˆ์‹œ, Java์—์„œ๋Š” ์ง€์›๋˜์ง€ ์•Š์Œ)

void modifyValue(int* num) {
    *num = 20; // Directly modifies the original variable
}

int main() {
    int number = 10;
    modifyValue(&number);
    printf("Original number: %d", number); // 20
    return 0;
}

 

์„ค๋ช…

 

  • modifyValue ํ•จ์ˆ˜์— number์˜ ์ฃผ์†Œ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค
  • ํ•จ์ˆ˜ ๋‚ด์—์„œ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›๋ž˜์˜ number ๊ฐ’์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Java์˜ ์ „๋‹ฌ ๋ฐฉ์‹

Java๋Š” ํ•ญ์ƒ Pass by Value ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋Š” Java์—์„œ ๋ชจ๋“  ๋ณ€์ˆ˜, ๊ธฐ๋ณธํ˜• ๋˜๋Š” ๊ฐ์ฒด ์ฐธ์กฐ, ๊ฐ’์˜ ๋ณต์‚ฌ๋ณธ์„ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ์‹ ๋•Œ๋ฌธ์— ๋งˆ์น˜ Pass by Reference์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค๊ณ  ์˜คํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ธฐ๋ณธํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…

๊ธฐ๋ณธํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…(int, double, char ๋“ฑ)์€ ์‹ค์ œ ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ์ „๋‹ฌ๋œ๋‹ค.

public class BasicTypeExample {
    public static void main(String[] args) {
        int a = 5;
        modifyPrimitive(a);
        System.out.println(a); // 5
    }

    public static void modifyPrimitive(int x) {
        x = 10; // Changes local copy only
    }
}

 

๊ฐ์ฒด ์ฐธ์กฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…

๊ฐ์ฒด ์ฐธ์กฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…(๋ชจ๋“  ํด๋ž˜์Šค ํƒ€์ž…)์€ ์ฐธ์กฐ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ์ „๋‹ฌ๋œ๋‹ค. ์ด ์ฐธ์กฐ๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ณต์‚ฌ๋œ ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ์›๋ž˜ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}
public class ObjectReferenceExample {
    public static void main(String[] args) {
        Person person = new Person("Alice");
        modifyObject(person);
        System.out.println(person.name); // Bob
    }

    public static void modifyObject(Person p) {
        p.name = "Bob"; // Modifies the object that p refers to
    }
}

 

 

์„ค๋ช…

 

  • modifyObject ๋ฉ”์„œ๋“œ์— person ๊ฐ์ฒด์˜ ์ฐธ์กฐ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.
  • p.name = "Bob";์€ person์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹ค์ œ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

Java์—์„œ์˜ Pass by Value ํŠน์„ฑ

๊ฐ์ฒด ์ฐธ์กฐ์˜ ๋ณต์‚ฌ

๊ฐ์ฒด ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๊ฐ€ ๋ณต์‚ฌ๋œ๋‹ค.

๊ฐ์ฒด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ์กฐ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€

๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ ์›๋ž˜ ์ฐธ์กฐ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

 

์˜ˆ์ œ

class Car {
    String model;

    Car(String model) {
        this.model = model;
    }
}

public class JavaPassByValue {
    public static void main(String[] args) {
        Car car = new Car("Toyota");
        changeReference(car);
        System.out.println(car.model); // Toyota
        changeObject(car);
        System.out.println(car.model); // Honda
    }

    public static void changeReference(Car c) {
        c = new Car("Honda"); // New reference, does not affect original
    }

    public static void changeObject(Car c) {
        c.model = "Honda"; // Changes the object referred to by c
    }
}

 

์„ค๋ช…

 

  • changeReference ๋ฉ”์„œ๋“œ๋Š” Car ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ์ฐธ์กฐ๋ฅผ ํ• ๋‹นํ•˜์ง€๋งŒ, ์ด ๋ณ€๊ฒฝ์€ main ๋ฉ”์„œ๋“œ์˜ car ๊ฐ์ฒด์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.
  • changeObject ๋ฉ”์„œ๋“œ๋Š” car ๊ฐ์ฒด์˜ model ์†์„ฑ์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

Java์˜ Pass by Value์™€ Pass by Reference ์ •๋ฆฌ

 

  • Java๋Š” ํ•ญ์ƒ Pass by Value๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๊ฐ์ฒด ์ฐธ์กฐ๊ฐ€ ์ „๋‹ฌ๋  ๋•Œ ์ฐธ์กฐ์˜ ๊ฐ’(์ฃผ์†Œ)์ด ๋ณต์‚ฌ๋œ๋‹ค.
  • ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฐธ์กฐ ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†๋‹ค.

ChatGPT ๊ธ€์˜ ์ถœ์ฒ˜

์ถœ์ฒ˜

  • Java Language Specification - Java์—์„œ์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ฐฉ์‹ ์„ค๋ช…
  • Effective Java by Joshua Bloch - Java์˜ ๊ฐ์ฒด ์ฐธ์กฐ์™€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ์— ๋Œ€ํ•œ ์„ค๋ช…
  • Java Programming Language Guide - Pass by Value์™€ Pass by Reference์˜ ์ฐจ์ด์  ์„ค๋ช…
 

Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Class

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 

Passing Reference Data Type Arguments
์ฐธ์กฐ ๋ฐ์ดํ„ฐ ์œ ํ˜• ์ธ์ˆ˜ ์ „๋‹ฌ

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

๊ฐ์ฒด์™€ ๊ฐ™์€ ์ฐธ์กฐ ๋ฐ์ดํ„ฐ ์œ ํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜๋„ ๊ฐ’๋ณ„๋กœ ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋  ๋•Œ ์ „๋‹ฌ๋œ ์ฐธ์กฐ๋Š” ์—ฌ์ „ํžˆ ์ด์ „๊ณผ ๋™์ผํ•œ ๊ฐœ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ ์ ˆํ•œ ์•ก์„ธ์Šค ์ˆ˜์ค€์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐœ์ฒด ํ•„๋“œ์˜ ๊ฐ’์€ ๋ฉ”์†Œ๋“œ์—์„œ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.