About self-increment ++ self-decrement in java--related content

You can use the online java editor for verification: https://c.runoob.com/compile/10/
Summary: Regarding the difference between the two, you only need to remember one sentence: i++ first participates in the operation and then adds 1; ++i It is to add 1 to itself first and then participate in the operation.

i++;先算再加1,但是不会返回结果,只有当你引用的时候会+1
++i;先减一再算
public class HelloWorld {
    
    
    public static void main(String []args) {
    
    
		int a = 10;
		int b = 20;
		int c = 25;
		int d = 25;
			//
       System.out.println("输出++a:   " + (++a));
		System.out.println("此时输出a  :     " + a);
	   System.out.println("输出--a:   " + (--a));
		System.out.println("此时输出a  :     " + a);
	   System.out.println("输出a++:   " + (a++));
		System.out.println("此时输出a  :     " + a);
	   System.out.println("输出a--:   " + (a--));
	   System.out.println("此时输出a  :     " + a);
		//查看d++ 与++d的不同
		 System.out.println("输出d++:   " + (d++));
		 System.out.println("此时输出d  :     " + d);
		 System.out.println("输出++d:   " + (++d));
		 System.out.println("此时输出d  :     " + d);
    }
}
输出++a:   11
	此时输出a  :     11
输出--a:   10
	此时输出a  :     10
输出a++10
此时输出a  :     11
输出a--11
	此时输出a  :     10
输出d++25
	此时输出d  :     26
输出++d:   27
	此时输出d  :     27

Example
Insert image description here

Guess you like

Origin blog.csdn.net/budaoweng0609/article/details/129625344