New practice of creating a new facility (for this situation)

I have a basic question regarding the code I'm learning from a book. I am new to Java, so I want to learn about best practice here.

Please note the following: https://www.refheap.com/89409

This is just a fragment of this class. There is a line of code:

Vector2f earth = earthMat.mul(new Vector2f()); 

I rewrote it as follows, and everything worked just fine:

 Vector2f earth = new Vector2f(); earth = earthMat.mul(earth); 

Is the first statement a more optimized approach? Being new to Java, I'm just trying to figure out if it really is better than the other. Systematically (at least in my opinion) these two statements are easier to digest right now. I like to specifically make calls against the name of the object.

+5
source share
2 answers

No silly questions.

But the answer is no, the only difference is that the second is easier to read, but also to get more work for the developer.

At compiler level, there is still a pointer to the object assigned to the mul method. The compiler does not care if it is in one or two lines.

+6
source

Speaking

 Vector2f earth = new Vector2f(); 

You create an earth link to point to an object of type Vector2f

Speaking

 Vector2f earth = earthMat.mul(new Vector2f()); 

the compiler creates an unknown link for you when you say new Vector2f() inside mul() . There are NO considerations in this context, but the second approach (as you mentioned, this is easier to learn) is more readable.

+3
source

Source: https://habr.com/ru/post/1201010/


All Articles