I have a basic question regarding some code I'm studying from a book. I am new to the Java language so I want to know the best practice here.
Reference the following: https://www.refheap.com/89409
This is just a snippet of the class. There is a line of code in there:
Vector2f earth = earthMat.mul(new Vector2f());
I rewrote as the following and all worked just fine:
Vector2f earth = new Vector2f();
earth = earthMat.mul(earth);
Is the first statement a more optimized approach at doing this? Being new to Java, I'm just trying to understand if one is really better than the other. Systematically, (at least in my mind), the two statements are easier to digest right now. I like to specifically make calls against the object name.
Avoid Creating Objects or Performing Operations That May Not Be Used. Replace Hashtable and Vector With Hashmap, ArrayList, or LinkedList If Possible. Reuse Objects Instead of Creating New Ones If Possible. Use Stringbuffer Instead of String Concatenation.
There are no stupid questions.
But the answer is no, the only difference is that the second one is more easily readable, but also more work typing for the developer.
At compiler level there is still a pointer to an object given to the mul
method. The compiler doesn't care if it's in one or two lines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With