-1

Please look at the following code :

public static void main(String[] args) {
    Random random = new Random();
    int[] array = new int[10];
    Arrays.setAll(array, operand -> random.nextInt(10));
    System.out.println(Arrays.toString(array));
    swap(array, 0, 9);
    System.out.println(Arrays.toString(array));
}

static void swap(int[] array, int i, int j) {
    int temp = array[i]; // pass by value ??
    array[i] = array[j]; // the value of temp doesn't change, why?
    array[j] = temp; // temp == array[i]
}

What exactly happens in the method swap?

I need a full explanation and a low level.

EDIT :

OK, let me show you another example :

public class StringHolder {

    private String value;

    public StringHolder(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return getValue();
    }

}

main method :

public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0].setValue("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}

output :

string 2
string 2

According to @chokdee's answer, tmp is a new variable and have it's own piece of memory...

but when we apply changes to the original variable (holder[0]), it also affects tmp.

another example :

public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0] = new StringHolder("string 2");
    System.out.println(tmp);
    System.out.println(holders[0]);
}

output :

string 1
string 2

Thanks in advance.

FaNaJ
  • 1,329
  • 1
  • 16
  • 39

4 Answers4

1

Ok trying to explain on a low level.

int temp = array[i]; // storing the value in a new variable

array[i] = array[j]; // the value of temp wan't changes because this is a NEW variable and have it's own piece of memory (It's no a reference or pointer like in C)

array[j] = temp; // here the value inside the array is assigned with saved value in temp
chokdee
  • 446
  • 4
  • 13
1

I'll answer your edit question.

public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0].setValue("string 2");
    System.out.println(tmp); // string 2
    System.out.println(holders[0]); // string 2
}

Because both are holding a reference to the same object.


public static void main(String[] args) {
    StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")};
    StringHolder tmp = holders[0];
    holders[0] = new StringHolder("string 2");
    System.out.println(tmp); // string 1
    System.out.println(holders[0]); // string 2
}

In this case the reference is hold by tmp. After you assign a new StringHolder object to holders[0] the reference in tmp is turned to the actual object. Normally, if you did not have a tmp the Garbage Collector would remove the object. In the end you have two different StringHolder objects.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

The item in the array at position i is swapped with the item at position j. So the item formerly known as array[i] will be found at array[j] after the method is returned. And the item formerly known as array[j] will be found at array[i] after the method returned.

Also the temp variable is used as swap space.

Juru
  • 1,623
  • 17
  • 43
0
int temp = array[i]; // pass by value ??

copies the element at position i to temp.

array[i] = array[j]; // the value of temp doesn't change, why?

copies the value at position j to position i. temp doesn't change since it's a variable not accessed in this statement. temp is a copy and not a alias for array[i].

array[j] = temp; // temp == array[i]

copies the value that was at position i before the assignment in the previous line to position j. temp == array[i] if and only if array[i] == array[j] at the beginning of the method.

fabian
  • 80,457
  • 12
  • 86
  • 114