Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java function parameters always passed-by-value for arrays? [duplicate]

Possible Duplicate:
Is Java “pass-by-reference”?

if we have big byte[] array (like 40Mb) and we want to send it in method

method(array);

will the array be copied? So memory will increase by another 40Mb in Java env => 80Mb, right?

If yes, how can we destroy the 'first' array after calling the method?

like image 465
VextoR Avatar asked Dec 27 '22 13:12

VextoR


1 Answers

No, the array will not be copied.

In Java, everything is always passed by value.

Variables of non-primitive types are references to objects. An array is an object, and a variable of an array type is a reference to that array object.

When you call a method that takes a non-primitive type parameter, the reference is passed by value - that means, the reference itself is copied, but not the object it refers to.

like image 107
Jesper Avatar answered Mar 04 '23 19:03

Jesper