ArrayList, HashMap의 Call By Value, Call By Reference 참조변수 테스트



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.ArrayList;
import java.util.HashMap;
 
import sun.text.CompactShortArray.Iterator;
 
 
public class AddressTest {
    
    public static class CallByValue{
        public int a;
    }
 
    public static void CallByReference(CallByValue param){
        param.a = 100;
    }
    
    public static void main(String[] args) {
        
        // ---------------------------------- #case1----------------------------------------------
        
        ArrayList<Integer> myArrayList1=new ArrayList<Integer>();
        ArrayList<Integer> myArrayList2=new ArrayList<Integer>();
        
        myArrayList1.add(10);
        myArrayList1.add(20);
        myArrayList1.add(30);
        // myArrayList1 = 10,20,30
 
        myArrayList2 = myArrayList1;
        
        myArrayList2.add(40);
        myArrayList2.add(0,5);
        myArrayList1.add(50);
        // myArrayList2 = 5,10,20,30,40
        
        System.out.println("myArrayList1값: "+myArrayList1); // 5,10,20,30,40
        System.out.println("myArrayList2값: "+myArrayList2); // 5,10,20,30,40
        
        // ---------------------------------- #case2----------------------------------------------
        
        HashMap<Integer, String> myHashMap1 = new HashMap<Integer, String>();
        HashMap<Integer, String> myHashMap2 = new HashMap<Integer, String>();
 
        myHashMap1.put(3"사이다");
        myHashMap1.put(1"커피");
        myHashMap1.put(2"우유");
        System.out.println("myHashMap1값: "+myHashMap1); // myHashMap1값: {3=사이다, 2=우유, 1=커피}
        myHashMap2 = myHashMap1;
        
        myHashMap2.put(4,"콜라");
        myHashMap2.put(5,"보리차");
        
        System.out.println("myHashMap1값: "+myHashMap1); // myHashMap1값: {5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}
        System.out.println("myHashMap2값: "+myHashMap2); // myHashMap2값: {5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}
        System.out.println(myHashMap2.keySet()); // [5, 4, 3, 2, 1]
        
        java.util.Iterator<Integer> myIterator = myHashMap2.keySet().iterator();
        while(myIterator.hasNext()){
            Integer key = myIterator.next();
            System.out.println(key+myHashMap2.get(key));
        }
        
        // ---------------------------------- #case3----------------------------------------------
        
        int a = 1;
        int b;
        
        b = a;
        b = 10;
        
        System.out.println("a값: "+a); // a = 1
        System.out.println("b값: "+b); // b = 10
        
        
        // ---------------------------------- #case4----------------------------------------------
        
        String c = new String();
        String d = new String();
        
        c = "ccc" ; 
        d = c ;
        d = "ddd";
        
        System.out.println("c값: " + c); // c = ccc
        System.out.println("d값: " + d); // d = ddd
        
        // ---------------------------------- #case5----------------------------------------------
        
        CallByValue callbyValue1 = new CallByValue();    
        callbyValue1.a = 200;
        System.out.println("callbyValue1값: "+callbyValue1.a); // 200
        CallByReference(callbyValue1);
        System.out.println("callbyValue1값: "+callbyValue1.a); // 100
    }
 
}
cs


case 1을 보면 myArrayList1과 myArrayList2는 서로 다른 변수로 선언 되어 있고


myArrayList1의 값을 add 해주고 myArrayList2 = myArrayList1 로 myArrayList1의 주소를 참조하고 있다.


그리고 myArrayList2의 값을 add해주고 myArrayList1값과 myArrayList2값을 찍어주면


같은 값(5,10,20,30,40) 이 출력된다.



case 2도 HashMap을 사용했는데 myHashMap2 = myHashMap1 로 주소를 참조해


myHashMap1과 myHashMap2의 같은 값{5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}이 출력된다.




case 3case 4 call by value 로 객체를 생성해 값을 집어넣거나 값을 직접적으로 넣어도 서로 다른 값이 나오게 된다.



case 5는 메소드를 타게하여 주소로 접근해 값을 변경시키고 있다.



+ Recent posts