Q:

why doesn't a loop show variable re declaration error

for(int i=0;i<2;i++){
        int j=i;
        System.out.println(j);
    }

    //same thing what a for loop does
/**********************************************/    
    int j=0;
    System.out.println(j);

    int j=1;
    System.out.println(j);

I got this silly doubt: I am trying to do the same thing what a for loop does by writing the same statements in a for loop multiple times but after declaring variable j for the second time the compiler shows variable redeclaration error but the this error does not show up in a for loop even its trying to execute the same statements multiple times  why?

A:

This is because the scope of the variable j is limited to a single iteration of the loop. Your second code is not entirely equivalent to your first "unrolled" loop. If you would like to make a complete equivalent of it, add curly braces around each simulated iteration, like this:
{
    int j=0;
    System.out.println(j);
}
{
    int j=1;
    System.out.println(j);
}

Now your code will compile and run correctly, producing identical results to the original code snippet.

