1

This should be simple related to scope, but I still don't understand what's going on the variable.

I made three methods in class containing a main method for the purpose of practice. Inside the main method, I declared two variables to use for trials: arr1 and a. Also, I declared two variables to compare with the original variables (arr1 and a): sorted and b.
Then, I tried to print the array by using printq method that I implemented to see what's difference between before and after.

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Main main = new Main();
    int[] arr1 = new int[]{29, 64, 73, 34, 20};
    int[] a = new int[]{2, 2, 2};
    int[] sorted = main.insertionSort(arr1);
    int[] b = main.changeValue(a);
    main.printq(arr1);
    main.printq(sorted);
    main.printq(a);
    main.printq(b);
}

^ This is the main method I tried.

    public int[] insertionSort(int[] arr) {
    int min, temp;
    for (int i = 0; i < arr.length - 1; i++) {
        min = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[min]) {
                min = j;
            }
        }
        temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }
    return arr;
}
public void printq(int[] a) {
    if (a == null) {
        System.out.println("[]");
    } else {
        String temp = "[";
        for (int i = 0; i < a.length - 1; i++) {
            temp = temp + a[i] + ", ";
        }
        temp = temp + a[a.length - 1] + "]";
        System.out.println(temp);
    }
}
public int[] changeValue(int[] c) {
    for (int i = 0; i < c.length; i++) {
        c[i] = i + 1;
    }
    return c;
}

this is methods that I tried.

main.printq(arr1) prints [20, 29, 34, 64, 73]. (supposed to be [29, 64, 73, 34, 20]) 
main.printq(sorted) prints [20, 29, 34, 64, 73] as well. (as I expected) 
main.printq(a) prints [1, 2, 3]. (supposed to be [2, 2, 2])
main.printq(b) prints [1, 2, 3]. (as I expected)

Even though the variables declared in main method are not static variables, this examples shows that one seems to be affected by another.

I tried to understand this in relation to access modifier and scope, but I ended up posting this because I still don't understand why int array arr1 and sorted have the same array at the end.
Can someone explain how this code works like this?

michaelitoh
  • 2,317
  • 14
  • 26
coborie
  • 13
  • 3
  • 1
    You should read https://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java to understand why this code works like this. – EkremC. Sep 22 '18 at 22:03
  • There was misunderstanding of the concept in java, which Java is always pass by value. The links were really helpful! Thank you so much. – coborie Sep 22 '18 at 23:07

1 Answers1

1

to avoid sending a reference of the original object data you could send a reference of its cloned (copy) object data.

    public static void main(String[] args) {
// TODO Auto-generated method stub
Main main = new Main();
int[] arr1 = new int[]{29, 64, 73, 34, 20};
int[] a = new int[]{2, 2, 2};
//sorted and b would now point to the cloned data of arr1 and a respectively
int[] sorted = insertionSort(arr1.clone());
int[] b = changeValue(a.clone());
main.printq(arr1);
main.printq(sorted);
main.printq(a);
main.printq(b);

}

kamyar haqqani
  • 748
  • 6
  • 19
  • Thank you! This is really helpful to understand how passing an array as parameter to a method would work. I thought Java is always pass by value, so I couldn't even think that the argument can be the value that has the reference of the array. – coborie Sep 22 '18 at 23:02