1

I was reading this and wondered whether there is a particular reason for having methods which fill a pre-existing array or object with data, rather than returning an instance of the object or data.

In some cases, such as graphics it allows an object to be re-used only rewriting the primitives without allocating another object on each call.

Are there any other benefits which I am not considering?

Community
  • 1
  • 1
Theo Pearson-Bray
  • 775
  • 1
  • 13
  • 32

3 Answers3

2

I was reading this and wondered whether there is a particular reason for having methods which fill a pre-existing array or object with data, rather than returning an instance of the object or data.

These are two alternative ways.
Providing a object to be populated as parameter of a method has some advantages when (there are of course others):

1) you want to provide to the method an object with a state defined by the client and not in a generic or pristine state defined in the invoked method.
Without passing an instance of it as parameter, you cannot do it.

2) you want to spare memory consumption. If you have already an object to pass it as parameter, why creating a new one in the invoked method ?

3) you have multiple objects to value in the method but a method can only return a single object.

Imagine you want to populate 3 lists in the implementation.

For example you could invoke poupulateList(myList, myOtherList, myAgainOtherList);

with this implementation :

public void populateList(List<String> list, List<String> myOtherList, List<String> myAgainOtherList){
       ....
}

Without passed the objects as parameters you are forced to introduce a custom class that contains all these objects and to define it as the returned type of the method.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

Consider the following simplistic method, which fills an integer array with a few numbers:

public void fillArray(int[] array) {
    array[0] = 0;
    array[1] = 1;
    array[2] = 2;
}

// now use it
int[] array = new array[3];
fillArray(array);

When the method fillArray() populates the array with some values, it is addressing the actual array passed in by the caller. The caller retains the reference to his array, and hence there is no need to pass a reference back to the data. Note that we could change the signature of fillArray() to int[], and return a reference to the array, but functionally speaking it would not make much difference.

Where returning an int[] might make sense is if we wanted to make a method which allocated space for an array and then returned the result, e.g.

public int[] getArray() {
    int[] array = new int[3];
    array[0] = 0;
    array[1] = 1;
    array[2] = 2;
    return array;
}

In this case, the caller doesn't have/want any exposure to workings of the array, he only wants the final product. Therefore, returning a reference to the array makes sense, without which the caller could not use the data generated by the method.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I understand the the use in terms of a function which modifies the contents of an object, e.g. Arrays.fill, the case I don't fully understand is when a method which modifies content acts as a getter. See https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[]) which effectively gets the values for the position of the view, but modifies the contents of an existing array rather than returning one. – Theo Pearson-Bray May 21 '17 at 14:11
  • @TheoPearson-Bray Which exact method are you referring to? – Tim Biegeleisen May 21 '17 at 14:12
  • getLocationOnScreen should be in the link. It fills an int array with the x and y positions. – Theo Pearson-Bray May 21 '17 at 14:14
  • @TheoPearson-Bray This method seems to exactly fit the description in my answer. The user has a 2D array which the `getLocationOnScreen()` method then populates. With regard to what is actually stored internally by the `View`, it doesn't really matter. We could look into the source code if you are really curious. – Tim Biegeleisen May 21 '17 at 14:18
  • I realise that the context of the method doesn't matter, it was just an example. Suppose I were writing a class, the user of which will need to access some information from. In which case would I write a getter, and in which would I write a method to populate an existing data structure? And why not write both? Is this just a choice based on how and how often the user of the class is expected to be accessing the data? – Theo Pearson-Bray May 21 '17 at 14:30
  • @TheoPearson-Bray Typically you would use a _setter_ to populate the array state of a class, not a getter. – Tim Biegeleisen May 21 '17 at 14:33
  • Sorry about all the questions but I'm somewhat confused. A setter method used on an object which the user already has a reference to might do something like modify alpha values in an image. The "setter" method I am confused about is one which sets values on an existing object in order to give the user access to the internal state of another object. In the case above if I want to know the location of a view on screen I need to create an int array which is then populated by the view object. In a case where I only use the value once it turns a getter into an assignment and a call. – Theo Pearson-Bray May 21 '17 at 14:39
  • @TheoPearson-Bray I am not sure that I follow your concerns. Maybe the other two answers address what you are looking for. – Tim Biegeleisen May 21 '17 at 14:42
  • That was my original thought, especially in graphics related code which is likely to be re-used. Thanks for the time taken to answer this. – Theo Pearson-Bray May 21 '17 at 14:43
  • @TheoPearson-Bray I had deleted this comment, but off the top of my head, perhaps there is a memory footprint reason for why Android doesn't want a `View` creating such an array willy-nilly and instead asks the caller to pass it in. I guess it would _really_ rather just pass back to primitive `int`, but this isn't possible from a single function/method, hence it uses an array instead to hold both x,y values. – Tim Biegeleisen May 21 '17 at 14:45
1

I understand why you are wondering. The design of MemoryInfo and getMemoryInfo() allow you to create as many MemoryInfo objects as you want, but if they were all to be correct, they should all hold the same data, so it would not really make sense to do. A clearer design would give you four getters that would return the four values.

I suppose that one reason for the chosen design is efficiency: it allows you to pass the same object to the method on every call, and it gives you all the information in one call instead of four. I am guessing here, though.

In general there are more things to consider when choosing one design over another. For example, if a method is to give you some information about a person, I would find it natural to pass a person object in for the method to modify rather than the method returning a new person object representing the same person every time called. Or at least it should give you the same object each time, only a new one when you inquire about a new person.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161