Arrays are passed by value or passed by reference in Java

662    Asked by FerminaZamorano in Salesforce , Asked on Jul 15, 2021

Why are arrays passed by reference?

Answered by Chris EVANS

 Arrays are not a primitive type in Java, but they are not objects either.

In fact, all arrays in Java are objects. Every Java array type has java.lang.Object as its supertype, and inherits the implementation of all methods in the Object API.

Like all Java objects, arrays are passed by value ... but the value is the reference to the array.

Real passing by reference involves passing the address of a variable so that the variable can be updated. This is NOT what happens when you pass an array in Java.

Because arrays are already pointers, there is usually no reason to pass an array explicitly by reference. For example, parameter A of procedure zero above has type int*, not int*&. The only reason for passing an array explicitly by reference is so that you can change the pointer to point to a different array.



Your Answer

Interviews

Parent Categories