Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous variables (?) advantages?

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.

like image 609
magicleon94 Avatar asked Feb 10 '17 09:02

magicleon94


People also ask

What are anonymous variables?

Variables without names are called anonymous variables.

Why are anonymous functions frequently used with event handlers select all that apply?

More generally, the point of using anonymous functions is that they do not require a name, because they are "event handlers" bound to a specific event on a specific object. In this case, the object is the entire Document Object Model, and the anonymous function executes when the entire DOM has loaded.

What is anonymous object in Java?

Anonymous means Nameless. An anonymous object is basically a value that has been created but has no name. Since they have no name, there's no other way to refer to them beyond the point where they are created.


1 Answers

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.

like image 90
Suresh Atta Avatar answered Sep 28 '22 06:09

Suresh Atta