Notice
Recent Posts
Recent Comments
Link
«   2024/03   »
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 29 30
31
Archives
Today
Total
관리 메뉴

pungjoo

reference VS value 본문

JAVA

reference VS value

pungjoo.kim 2007. 2. 7. 15:39
들어가면서
자바에서 call by reference일까요 call by value일까요?

예제 소스
     1  public class CBRTest {
     2
     3          static void change(StringBuffer r) {
     4                  r.delete(0, 1);
     5                  r.append("10");
     6          }
     7
     8          static void change2(StringBuffer r) {
     9                  r = new StringBuffer("15");
    10          }
    11          public static void main(String [] args) {
    12                  StringBuffer s = new StringBuffer("5");
    13                  System.out.println(s);
    14                  change(s);
    15                  System.out.println(s);
    16                  change2(s);
    17                  System.out.println(s);
    18
    19          }
    20  }

결과
5
10
10

메모리 구조로 풀어본 value
A / line 12 : new StringBuffer("5")
              default 크기만큼의 Memory를 할당 받아 0x35h(문자 5를 의미함)를 기록함..
              이 문자열은 memory address 0x0000h ~ 0x000fh를 차지함.

B  / line 12 : StringBuffer s =
               s를 의미하는 memory address 0x0100h에 A에서 얻은 주소의 시작점을 기록함..
               즉, 0x0100h에는 0x0000h가 들어 감.

//change(s);
C / line  3 : StringBuffer r
              r을 의미하는 memory address 0x0200h에 0x0000h를 기록함
    line  4 : r(0x0200h)이 가르키고 있는 0x0000h의 memory에서 문자 삭제 ( delete ... )
    line  5 : r(0x0200h)이 가르키고 있는 0x0000h의 memory에서 문자 추가 ( append ... )

//change2(s);
D / line  8 : StringBuffer r
              r을 의미하는 memory address 0x0300h에 0x0000h를 기록함

E / line  9 : new StringBuffer("15");
              default 크기만큼의 Memory를 할당 받아 0x31h, 0x35h(문자 1과5를 의미함)를 기록함..
              이 문자열은 memory address 0x0400h ~ 0x040fh를 차지함.

F / line  9 : r =
              D에서 정의한 r의 주소에 E에서 얻은 주소의 시작점을 기록함.
              즉, 0x0300h에는 0x0400h가 들어 감.

메모리 구조로 풀어본 reference
A / line 12 : new StringBuffer("5")
              default 크기만큼의 Memory를 할당 받아 0x35h(문자 5를 의미함)를 기록함..
              이 문자열은 memory address 0x0000h ~ 0x000fh를 차지함.

B  / line 12 : StringBuffer s =
              s를 의미하는 memory address 0x0100h에 A에서 얻은 주소의 시작점을 기록함..
              즉, 0x0100h에는 0x0000h가 들어 감.

//change(s);
C / line  3 : StringBuffer r
              r을 의미하는 memory address는 실 매개 변수(actual parameter)와 동일하므로 0x0100h address를
              그대로 사용함.
    line  4 : r(0x0100h)이 가르키고 있는 0x0000h의 memory에서 문자 삭제 ( delete ... )
    line  5 : r(0x0100h)이 가르키고 있는 0x0000h의 memory에서 문자 추가 ( append ... )

//change2(s);
D / line  8 : StringBuffer r
              r을 의미하는 memory address는 실 매개 변수(actual parameter)와 동일하므로 0x0100h address를
              그대로 사용함.

E / line  9 : new StringBuffer("15");
              default 크기만큼의 Memory를 할당 받아 0x31h, 0x35h(문자 1과5를 의미함)를 기록함..
              이 문자열은 memory address 0x0400h ~ 0x040fh를 차지함.

F / line  9 : r =
              D에서 정의한 r의 주소에 E에서 얻은 주소의 시작점을 기록함.
              즉, 0x0100h에는 0x0400h가 들어 감.

@
Comments