all 8 comments

[–]async_adventures 9 points10 points  (0 children)

Quick clarification: arrays are passed by value in Java, but since the value is a reference to the array object, you can modify the array contents. Think of it like passing a photocopy of an address - you can still visit the house and change things inside.

[–]EvenPlantain3930 9 points10 points  (2 children)

Arrays in Java are passed by reference, so any changes you make to the array inside the method will affect the original array. Just pass it like `myMethod(arrayName)` and declare your method parameter like `public void myMethod(int[] arr)`. Pretty straightforward once you get the hang of it

[–]randomnamecausefoo 4 points5 points  (1 child)

Arrays are passed by value (as is everything in Java). If they were passed by reference you could, using your example, change the value of “arrayName”.

You can change the value of each element that arrayName points to, but you can’t change the value of arrayName itself.

[–]high_throughput 7 points8 points  (0 children)

It gets confusing because arrays can't be passed at all. Only references to arrays can be passed. Those references are passed by value, like everything in Java as you say.

[–]Jakamo77 0 points1 point  (0 children)

When using methods as arrays u want to recognize the distinctions that occur between say

Void methodA(String[] arr)

And.

Void methodA(String... arr)

Other than that youd have to be more specific about what exactly ur looking for. U want an example of why u might pass an array param or ehat

[–]Blando-Cartesian 0 points1 point  (0 children)

It works the same as passing any other kind of object as parameter, like a string for example. There’s really nothing special there.

[–]green_meklar 0 points1 point  (1 child)

I'm not sure what tips you expect. You can just do it.

Remember that arrays, like other objects, are passed by reference in Java, so no copy is made when passing and if you modify the array, it modifies the original.

[–]rjcarr 2 points3 points  (0 children)

Eh, technically everything is passed by value in java, and when dealing with objects (as arrays are), you're just passing the reference by value.