Is declaring a variable inside a loop is good or declaring on the fly optimal in Java.Also is there any performance cost involved while declaring inside the loop?
eg.
Option 1: Outside the Loop
List list = new ArrayList();
int value;
//populate list
for(int i = 0 ; i < list.size(); i++) {
value = list.get(i);
System.out.println(“value is ”+ value);
}
Option 2: Inside the Loop
List list = new ArrayList();
//populate list
for(int i = 0; i < list.size(); i++) {
int value = list.get(i);
System.out.println(“value is ”+ value);
}