if we declare a variable before a for loop and we are declaring same name variable in for loop statement. we are getting error in java. why as both have different scope. why compiler is unable to recognize it.
public class Main {
public static void main(String[] args) {
int [] arr = {0,1,2,3,4,5,6,7,8,9};
int j = 0;
int i = 0; // first declaration
for(int i = arr.length-1,j = 0; (arr.length)/2<=i; i-- ) { // second declaration
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
for(int i = 0 ; i< arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println(j);
}
}