1

Consider the following two calls to the same method in java:-

1) doSomething(new Object[]{"something"}) ;

2)

Object[] obj = {"something"} ;

doSomething(obj);

Which one is more efficient in terms of memory and time efficiency ? I would say the 1) is better in both memory and time efficiency. Reason being in the second option requires us to create another variable (extra memory) and then assigns that value to the variable (extra time). Any comments ?

Just to clarify the object will be create only once, i am talking about the extra variable being used to hold the address of the newly created object.

1
  • 1
    Not to be nitpicking here but you create obj no matter what, it's a question of when/if obj will be garbage collected.
    – posdef
    Jan 14, 2013 at 10:10

3 Answers 3

4

Both are the same in terms of time and memory. The extra assignment can be optimized away by the compiler.

A difference is that the second version gives you an opportunity to give a useful name to your variable, which can make the code more clear.

2
  • I agree with you that the second might be clear. But, Lets say the actual address of the object is 42. In the first case, it will first assign that value to a variable obj (extra time) and then that value would be passed to the function. In the second case, the address of the object returned by the "new" operator will be direclty passed to the function. So it would be someting like for both the cases:- Jan 14, 2013 at 10:15
  • 1) doSomething(42); 2)obj = 42; doSomething(obj); Your point is valid only if Java is going to create an intermediate variable to hold the address and then pass that variable. I am not sure if Java does that. If yes, why it need to do that. Just pass the actual address ? Jan 14, 2013 at 10:35
2

The second call allows you to reuse the object in the calling method, but the first one does not.

It has no incidence on memory, as the passed object is created anyway.

3
  • But another variable is also created. Jan 14, 2013 at 10:36
  • No, in the second call, a variable is created, and then it is passed as a reference to the called method. But it remains the same object, which is created only once in memory. OK, it creates another address reference, but it's negligible both in time and memory, and can be optimized away à compile time, as Mark said. Jan 14, 2013 at 10:41
  • Yes i was talking about the extra address reference hold into the new variable (Actual object will be created only once) and Yes that would be negligible. Jan 14, 2013 at 10:53
1

You should always consider what is simpler and clearer first. You should only consider performance when you know you have a problem because you measured it in a profiler or micro-benchmark.

The best option is likely to be to use varargs

doSomething("something");

void doSomething(String... args) { }

Note: not only is the this simplest, but it is also potentially the fastest as the JIT can eliminate the String[] created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.