At this point, speed is not important (add a comment on premature optimization;). The important thing is how quickly you can understand what the code is doing , which should call the dicePool method once.
The first method allocates an array of size dicePool and dicePool over its values, which causes the dicePool loop body to run once (I will pretend that you had int in mind instead of Integer to avoid unrelated autoboxing problem). This is potentially inefficient for the computer on which the code is running, but more importantly, it is inefficient for the person reading the code because it is conceptually remote from what you wanted to execute. In particular, you make the reader think of the new array that you just created, and the value of the variable a , which will be 0 for each iteration of the loop, although none of them are related to your final goal.
Any Java programmer looking at the second method will understand that you execute the body of the dicePool cycle once from i "counting" to dicePool . Although the last part is not particularly important, the beginning is exactly what you wanted to do. Using this common Java idiom minimizes the unrelated things the reader should think about, so this is the best choice.
If in doubt, go with simplicity.: D
source share