6

I was wondering about one thing that jumped into my mind yesterday.
I apologize in advance for the misleading title, but I really don't know how to entitle this.

Well, suppose we are two objects ObjA and ObjB, and that, for instance, ObjB has a method that takes an ObjA object as argument.

We can do this (taking java as language):

ObjA instanceA = new ObjA();
ObjB instanceB = new ObjB();

instanceB.method(instanceA);

or

new ObjB().method(new ObjA());

Assume this is the body of some function, so the objects will be destroyed when going out of scope.

My question is:
do we get a performance advantage by not instantiating the singular objects and calling the implicitly as for the second code?
Is this readability sacrifice worth something?
Or is it all for nothing since the implicitly created objects will be stored in memory and die by scope anyway?


Note: I don't know if I'm saying right with "implicit" or "anonymous", but I have not found much on Google.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 2
    In Java, this should not matter performancewise. You can look at the generated bytecode for both versions to be sure. But as a general rule: use whatever is more readable. – Axel Feb 10 '17 at 09:32
  • 4
    You're still instantiating the same number of objects. The bits with the word `new` are the instantiations, even if you don't assign variables. – khelwood Feb 10 '17 at 09:33
  • 2
    There's no advantages whatsoever. Even if there were, it would be so minuscule that you'd be wasting your time concentrating on something like this. – Kayaman Feb 10 '17 at 09:33
  • 3
    It's not a realistic case. You wouldn't create an object just to pass to a method that has side-effects without wanting to observe those side-effects, and if the method doesn't have side-effects you shouldn't have to create an object for it at all. – user207421 Feb 10 '17 at 09:40
  • I know it's not realistic but I wanted to try to keep it simple. I've encountered this dilemma in some realistic cases, but the concept is the same – magicleon94 Feb 10 '17 at 09:41
  • @Alex would there be any difference if I used c++? – magicleon94 Feb 10 '17 at 09:43
  • 1
    @magicleon There is a difference in bytecode, but that gets JIT compiled, so the result will probably be exactly the same. – Jorn Vernee Feb 10 '17 at 09:46

3 Answers3

6

Absolutely no difference performance wise.

But in few cases, you will be forced to use first type.

For ex :

ObjA instanceA = new ObjA();
// Do something with instanceA
instanceB.method(instanceA);

If you have nothing to do in middle, I can just use the second way to save a line of code.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

In this case readability is the only advantage one way or another.

There's no significant performance or memory benefit.If you store a reference in a local variable and then invoke the method,there may be an extremely small performance penalty for storing the reference.

The anonymous object is created and dies instantaneously. But, still with anonymous objects work can be extracted before it dies like calling a method using the anonymous object:

 new ObjB().method(new ObjA());

We can’t use twice or more as the anonymous object dies immediately after doing its assigned task.

Mihir
  • 572
  • 1
  • 6
  • 24
1

The first approach using named variable is required if you need to store the object to use it several times without creating new objects or to store the current result of some method returning an object if there is a risk (e.g. because of multi-threading) the result will be different the very next time. In other cases it's more convenient to use anonymous objects since their scope is the same and there is no namespace cluttering with unneeded identifiers.

Gyrotank
  • 153
  • 1
  • 2
  • 9