Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Object creation best practice (for this situation)

Tags:

java

object

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.

like image 207
Adam Wilkins Avatar asked Aug 24 '14 07:08

Adam Wilkins


People also ask

What is not a good practice for creating objects?

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.


1 Answers

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.

like image 103
baklap Avatar answered Nov 07 '22 16:11

baklap