Java = Return a list of objects / array or result-object (same as method parameters)

This may seem like a strange question: I am struggling to decide whether it is good practice and to “work effectively” with “typed objects” at a very narrow level.

public Object[] doSomething() {
    Object[] resultList = new Object[] {new Foo(), new Bar()};
    return resultList;
}

against

public Result doSomething() {
    Result result = new Result();
    result.foo = new Foo();
    result.bar = new Bar();
    return result;
}

public class Result{
    Foo foo;
    Bar bar;
}

My question is specific:

  • In terms of CPU cycles (as a relative figure), how much of the second approach consumes more resources. (e.g. 100% more)

  • Same question regarding memory consumption

NB (these two questions, to understand this more, and not about premature optimization)

  1. " ". , 1 No-Go , ... " " ((( - )))...

, , ( ), ( ...)

public doSomething(Query query ) 

public doSomething(Foo foo, Bar bar, Aaaa, a, Bbbbb)

+3
3

, , , . . , : , Result. Result , , - . , , JVM ( , resultList [12]?), . , , (- ), , (- ). , .

, , .

, , - , , , , . , :

public Object[] getCustomerData(int customerid)
{
  String customerName=... however you get it ...
  BigDecimal currentDue=...
  BigDecimal pastDue=...
  return new Object[] {customerName, pastDue, currentDue};
}
... meanwhile, back at the ranch ...
Object[] customerData=getCustomerData(customerid);
BigDecimal pastDue=(BigDecimal)customerData[2];
if (pastDue>0)
  sendNastyCollectionLetter();

? №2 lastDue, # 1. , , , , . , # 14, # 15. , . , . .

, , - - , , . , , , , . , . , , , , .. ..

+1

3.) " ". , 1 No-Go, , ... " " ((( - langauge/ ...) ))...

1 . . , . , .

Object[], new Object().

+6

, Result , , Object[]. (, .) , ?

, :

  • .
  • .
  • "" .

:

public doSomething(Query query) 

public doSomething(Foo foo, Bar bar)

. Foo Bar Query , . (.. " " ), , , . , ( ) .

+2

All Articles