Java: getter method versus instance public variable: performance and memory

Sorry for the noob questions. Passing by reference and value is difficult!

So, I have a class with fairly large data structures - multidimensional arrays. I need to access these arrays from another class. I could just make the arrays public and make the classic objectWithStructures.structureOne. Or, I could do getters: adding a method such as public int [] [] [] getStructureOne ().

Does getter make a copy of multidimensional array? Or does he pass it by reference, and you just can’t change the link to the object?

I am worried about memory and performance. But creating public data structures, although faster if it doesn't cause copying, seems like bad coding practice.

ADDENDUM: Therefore, when I return a reference to an object (for example, an array) using the getter method, can this object be edited by those using the getter method? Or is it somehow "locked" for editing, so only its class can modify this object?

+6
source share
4 answers

In fact, in java, technically everything is passed by value because the link is the value of the memory address, however you can think of it as sending by reference.

As far as performance is concerned, I doubt that there is a measurable difference, because the JVM JIT compiler is likely to be built into the accessor method ("getter"). In terms of style, it is better to use getters, preferring to publish your fields.

As for secure publishing (allowing secure access to personal data) - no, the object is not "locked in read-only mode"; it is fully editable because arrays are mutable.

To safely allow access to your data array, basically you have two options:

  • returning a copy of the array from your getter is expensive
  • provides an API that returns an element at a given position - easy to code, but it may be harder to resize the array later because the API was defined for your class

An example of providing an API might be:

public int getStructureOne(int x, int y, int z) { return structureOne[x][y][z]; } 

This method is completely safe, because primitives (for example, int ) are passed by value - this caller changes the value of the variable to which the result of this method is assigned, nothing happens to the array.

+4
source

Passing by reference to a value is hard

In Java, everything is passed by value. Primitive types are passed by their value, complex types are passed by the value of their reference. Arrays are not primitive, so their reference is passed.

I could just make arrays public ... Or, I could make getters

If they are public , then other classes can change the reference to the array containing your object. If there are recipients that return a reference to the array, then callers can modify the contents of the array. Both are pretty bad. This also answers your add question.

I think the following options:

  • Use getters for individual cells, as @Bohemian suggests. If there is any lock on the array object, there may be additional parameters.

  • Return the link to the array and trust your subscribers not to spoil them. Additional checks can be implemented by checking access at compile time.

  • Maintain two copies of the array inside the object. One to return to the getter, and the other for the present work. You can document that changing the array is not affected and claims that their contents are the same when returning from getter.

+4
source

Everything is passed by value, but the receiver will return a reference to the array, without any copies.

anyone who changes will see everything with reference to this array.

as for 1) performance, the only way to find out is to navigate. the get method will add overhead, but if your structures are huge, any access to the elements in the structure will overshadow any overhead for the added method call.

as for 2) bad coding practice, it really depends on what you do. should your code ensure that it cannot be modified by anyone else? then return the copy. or better yet, force the caller to request a range of data and return them only a copy of the range that they need.

+1
source

Java conveys everything by value. Java links are pointers passed by value.

0
source

Source: https://habr.com/ru/post/925622/


All Articles