donaricano-btn

Call by value

- There is only call by value in java, not call by reference


1. Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test{
     
    int data = 50;
     
    void change(int data){
        data = data + 100;
    }
     
    public static void main(String[]args){
         
        Test obj = new Test();
        System.out.println(obj.data);
        obj.change(50);
        System.out.println(obj.data);
    }
}

50

50

- The value of data is not changed

2. Another Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package statics;
 
public class Test{
     
    int data = 50;
     
    void change(Test a){
        data = data + 100;
    }
     
    public static void main(String[]args){
         
        Test obj = new Test();
        System.out.println(obj.data);
        obj.change(obj);
        System.out.println(obj.data);
    }
}
 

- If we pass object in place of any primitive value, original value will be changed

- This case we are passing object as a value



'Back-End > Java_1' 카테고리의 다른 글

[Java] Life cycle of a thread  (0) 2016.10.05
[Java] Multithreading in java  (0) 2016.10.05
[Java] Encapsulation  (0) 2016.09.12
[Java] Package  (0) 2016.09.12
[Java] Interface  (0) 2016.09.12
블로그 이미지

리딩리드

,