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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!