32

Why does this

 int x = 2;
    for (int y =2; y>0;y--){
        System.out.println(x + " "+ y + " ");
        x++;
    }

prints the same as this?

 int x = 2;
        for (int y =2; y>0;--y){
            System.out.println(x + " "+ y + " ");
            x++;
        }

As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop?


  • for(initialisation; condition; incrementing): yes. - Mitch Wheat
  • Use javap to produce bytecode for both and see. Be an experimentalist. - duffymo

Linked


Related

Latest