memory management - How bad is declaring arrays inside a for loop in Java? -
I have come from a background, so I accept that I'm still writing in Java with memory management I'm struggling. Here's an issue that has come some time that I would love to get some detail. There are two ways to write the same routine, only difference when the code sample 1 : code sample 2 : here, What is the reason that they like more than other ways or Are they really similar in Java? Here's nothing special about Array because you are not allocating to the array, You are just creating a new variable, this is equal to: In a case where you make the variable out of the loop, the variable (which holds the position of the thing that represents it) will only be allocated once, in case where You make the loop in-loop, the variable can be allocated again for each repetition, but my guess is compiler or JIT will fix it in an optimization phase. I call it a micro-optimization, if you are participating in problems with this segment of your code, then you should make decisions based on the measurement rather than the specs, if you have this segment of code If you are not running into problems with it, then you should interpret the meaning of the right thing and variables in the field that makes sense. Also see. double [array] is declared :
double [] array; For (int i = 0; i & lt; n; ++ i) {array = calculation and adsorption by (i); If (some functions on array (array)) {// one thing} other {// any other thing}}
for
(int i = 0; i & lt; n; ++ i) {double [array] array = calculationSomethingAndReturnAnArray (i); If there are some functions (arrays) {// one thing} other {// any other thing}}
personal double [] calculate some other endorse I) Always give an array of the same length; I have a strong dislike for code sample 2 because it creates a new array for each walk, when it is going to present the current array Could have overwritten. However, I think it can be one of those times when I should just sit back and Java should handle the situation for me
object foo; For (...) {foo = func (...); }
Comments
Post a Comment