티스토리 뷰

JAVA

Java is Pass-By-Value

seoca 2019. 7. 27. 06:08

 

 

Primitive types swap method will never work because Java is strictly Pass-By-Value.

If you want to swap two arguments, use array or object to send reference as a value.

 

 

Example I

primitive types are never changed

class Main {
    //primitive type doesn't swap
    public static void swap(int i, int j)
    {
        int temp = i;
        i = j;
        j = temp;
    }
    public static void main(String[] args)
    {
        int i = 10;
        int j = 20;
        swap(i, j);
        System.out.println("i = " + i + ", j = " + j);
    }
}
 
//i = 10, j = 20

 

 

 

 

Example II

Reference types (Object, Array) are changed as they pass the reference as a value.

public class Main {
    public static void main(String[] args) {
        int arr[] = {32};
 
        passByValue(arr);
        printArray(arr);
    }
 
    private static void passByValue(int[] arr) {
        swap(arr, arr[0], arr[1]);
    }
 
    private static void swap(int[] arr, int source, int target) {
        int tmp = arr[0];
        arr[0= arr[1];
        arr[1= tmp;
    }
 
    private static void printArray(int[] arr) {
        for (int data : arr) {
            System.out.print(data + ", "); //2, 3
        }
    }
 

 

 

 

 

Example III

Even if you use an object type to change the value, swap will not work if the reference is changed to reference a new location or object.

class Test
{
    int x;
    Test(int i) { x = i; }
}
class Main
{
    public static void main(String[] args)
    {
        Test t = new Test(5);
 
        change(t);
        System.out.println(t.x);
    }
 
    public static void change(Test t)
    {
        t = new Test(0); //referred to other address so that it won't change the value
        t.x = 10;
    }
}
 

 

*pass-by-reference 로 주소(reference)를 전달했다면 값이 변했겠지만 자바는 철저히 pass-by-value 이기에 다른 객체를 선언했으면 값(value)이 변할 수 없다.

 

 

 

Example IIII

Check References to Objects

public class Pass {
    int x = 0;
    int y = 0;
    public Pass(int i, int j) {
        x = i;
        y = j;
    }
}
 
public class Main {
    public static void main(String[] args) {
        Pass ps1, ps2;
        ps1 = new Pass(1010);
        ps2 = new Pass(2020);
        ps2 = ps1;
        ps1.x = 30;
        ps1.y = 30;
        System.out.println(ps1 + " " + ps2); //Pass@61bbe9ba Pass@61bbe9ba
        System.out.println("Pass1: " + ps1.x + ", " + ps1.y); //Pass1: 30, 30
        System.out.println("Pass2: " + ps2.x + ", " + ps2.y); //Pass2: 30, 30
    }
}
 

 

The references are different without assigning.  

public class Main {
    public static void main(String[] args) {
        Pass ps1, ps2;
        ps1 = new Pass(1010);
        ps2 = new Pass(2020);
        //ps2 = ps1;
        ps1.x = 30;
        ps1.y = 30;
        System.out.println(ps1 + " " + ps2); //Pass@61bbe9ba Pass@610455d6
        System.out.println("Pass1: " + ps1.x + ", " + ps1.y); //Pass1: 30, 30
        System.out.println("Pass2: " + ps2.x + ", " + ps2.y); //Pass2: 20, 20
    }
}
 

 

 

 

 

Reference

http://www.informit.com/articles/article.aspx?p=174371&seqNum=4

https://www.geeksforgeeks.org/g-fact-31-java-is-strictly-pass-by-value/

 

'JAVA' 카테고리의 다른 글

The server time zone Error  (0) 2019.07.31
Connect to MySQL using Java JDBC  (0) 2019.07.31
Arrays.sort in Java  (0) 2019.07.23
Collection framework - HashMap  (0) 2019.07.23
keySet() in Java  (0) 2019.07.23