Important thoughts on i++ and ++i, and the use of ++ in functions

Important thoughts on i++ and ++i

First look at the following program:

public class PPTest {
    
    
    public static void main(String[] args) {
    
    
        int x=0;
        System.out.println("函数外x为:"+x);
        test(x++);
        System.out.println("现在x是:"+x);
        test(++x);
        System.out.println("现在x是:"+x);
    }

    public static void test(int x) {
    
    
        System.out.println("函数内x为:"+x);
    }
}

image-20210317174231290

See the problem?

Yes, in the function, the parameter is called ++, then the external data will also change

And, if it is 后++, then the internal function calls the ++previous value!

Such problems will have a significant impact when writing backtracking related problems! ! ! !



If you want to add to the function, it must be written i+1in the form

public class PPTest {
    
    
    public static void main(String[] args) {
    
    
        int x=0;
        System.out.println("函数外x为:"+x);
        test(x+1);
        System.out.println("现在x是:"+x);
    }

    public static void test(int x) {
    
    
        System.out.println("函数内x为:"+x);
    }
}

image-20210317174601579

md, I did not expect to be ++troubled by the problem at this time

Guess you like

Origin blog.csdn.net/weixin_44062380/article/details/114941036