Sobes.tech
Junior — Middle

Can you explain what is meant by the concept of Expression in programming?

sobes.tech AI

Answer from AI

In Java, parameters are passed by value, but for objects (including arrays), a copy of the reference to the object is passed. This means that if you modify the array's contents inside a method, these changes will be visible in the calling code because both refer to the same object.

However, if inside the method you assign a new array to the parameter (e.g., arr = new int[]{...}), this new reference will not affect the reference in the caller.

Example:

public void modifyArray(int[] arr) {
    arr[0] = 10; // modify array contents
    arr = new int[]{1, 2, 3}; // assign new array - will not affect external array
}

public static void main(String[] args) {
    int[] myArray = {0, 0, 0};
    modifyArray(myArray);
    System.out.println(myArray[0]); // prints 10
}

Thus, you cannot directly get a reference to a new array created inside a method from the caller, but you can modify the contents of the passed array.