I was wondering which one is better in term of memory allocation. I know at this scale these would not be any different since creating a variable would take so little memory, but I wanted to get used to coding the better way for the future.
public static void test(Scanner input, int[] arr){
for (int i = 0; i < 5; i++){
int age = input.nextInt();
arr[i] = age;
}
or
public static void test(Scanner input, int[] arr){
int age = null;
for (int i = 0; i < 5; i++){
age = input.nextInt();
arr[i] = age;
}
}
Declaring the variable before the for loop would only allocate one place in memory for all the values I set it to, correct? But if I declare the variable in the for loop, will 5 different places in memory be allocated or just one that will be overwritten when the for loop is run again?
I should also state that the variable age will not be used anywhere else but inside that for loop.
Thanks.