0

What is better in java, is it make any difference, if yes what difference?

public void att(){
        for(int i=0;i<size-1;i++){
            //Do things
        }
        for(int i=0;i<size-1;i++){
            //Do things
        }
}

or:

public void att(){
        int i;
        for(i=0;i<size-1;i++){
            //Do things
        }
        for(i=0;i<size-1;i++){
            //Do things
        }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Chichila
  • 109
  • 3
  • 1
    What do you mean by "better"? " is it make any difference" yes, method scope vs loop scope. Also remember that premature optimization is root of all evil. Write clear code and let [JIT](http://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do) handle optimization for you. – Pshemo Jan 26 '14 at 01:40
  • Certainly the second is better from a strict efficiency point of view, but we're talking infinitesimal improvements here. As far as maximizing the understandability of your code, the first is likely better, since 'i' should be limited to the scope in which it's used, and it isn't. – aliteralmind Jan 26 '14 at 01:41
  • In the case that you need the value of `i` after the loop ends, declaring `i` above the loop is required. For instance, tf you `break` out of the loop, you might need to use what is stored in `i`. For practical coding, it's a nice convenience to be able to declare the variable within the `for`. Scope always matters, too. I think it would be asking for trouble to have `int i; ... for ( i ... )` and later have `for (int i ...)`, – DSlomer64 Jan 26 '14 at 01:46
  • The code above *probably* has an off-by-one error because it fails to loop with the last element. The usual idiom is `for (i = 0; i < size; i++)` – pmg Mar 12 '14 at 00:27

3 Answers3

3

Like this I guess...

public void att(){
    int i; //Same variable
    for(i=0;i<size-1;i++){ //Same variable
        //Do things
    }
    for(i=0;i<size-1;i++){ //Same variable
        //Do things
    }
}

-

public void att(){
    for(int i=0;i<size-1;i++){ //Different variables
        //Do things
    }
    for(int i=0;i<size-1;i++){ //Different variables
        //Do things
    }
}

You declare one more variable with the same name, wich means that you clear the scope.

This could be a more constructive answer to your question:
Difference between declaring variables before or in loop?

Community
  • 1
  • 1
NULL
  • 313
  • 1
  • 12
1

The completely separate for loops - each with its own i variable - are better, because there is a general programming guideline that variables should have the narrowest scope that works. In your second example, the scope of i extends beyond where it is used.

There would be no performance difference between the two.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Never try to optimise your code in such ways if you're not having a specific problem with efficiency. Like Pshemo said: premature optimisation is the root of all evil.

The only real difference I see between the two, is that the second one is one line longer and therefore less readable.

Also, if anything, I'd say in the second case the variable is declared in a higher scope, which I think is actually a bad thing. Typically, you want to have variables declared in the scope that they are used in, nested as deep as possible and as such have the shortest possible lifespan.

Reinstate Monica
  • 2,767
  • 3
  • 31
  • 40