Java – anonymous variable (?) Advantages?

I want to know something suddenly came to my mind yesterday

We can do this (in Java):

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

instanceB.method(instanceA);

or

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

Suppose this is the body of a function, so when out of range, the object will be destroyed My question is: do we get a performance advantage by not instantiating a single object and implicitly calling the second code? Is this readability sacrifice worth it? Or does it have nothing because the implicitly created object will be stored in memory and still die in scope?

Note: I don't know if I mean "implicit" or "anonymous", but I can't find much on Google

Solution

There is absolutely no obvious difference in performance

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

For example:

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

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>