Thursday, 2 February 2017

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










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?


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...