October 8, 2012
Difference Between Variable Declared Inside For Loop and Outside Loop
Question by sagar
How does the jvm manage the memory for i
and j
? What is the difference between i
and j
other than their scope?
int i;
for(i=0;i<5;i++)
{
System.out.println(i)
}
for(int j=0;j<5;j++)
{
System.out.println(j)
}
Answer by Starx
Its basically the same thing. The only difference is the declaration happens before the loop in the first case and and in the second case it happens before the execution of the loop.
About JVM’s memory management, it is basically managed in the same way.