Detailed Explanation of Increment and Decrement Examples of Java Operators





Java provides a rich set of operators to manipulate variables. We can divide operators into the following groups:

Arithmetic operators
Relational operators Bitwise
operators
Logical operators
Assignment operators
Other operators

  • Increment/decrement

Java provides ++ operation and – operation, which can add 1 and subtract 1 to an integer:



	
public class Test01 {
    
    
	
	public static void main(String[] args) {
    
    
		
		/**
		 * 	等于号表示赋值的意思,即 右边赋值到左边
		 * 		i++  先赋值,后运算;
		 * 		++i  先运算,后赋值;
		 */
		int i = 0;
//		i = i++;//0
		i = ++i;//1
		System.out.print(i);
	}
}





public class Test02 {
    
    
	
	public static void main(String[] args) {
    
    
		
		//自增 ++ ;自减-- 
		/**
		 * 总结:
		 * 	 自增自减在变量后时,直接返回等于号左边值(注:等于号表示赋值从右往左)
		 * 	 自增自减在变量前时,等于号两边值都改变一致
		 * 
		 *   ++出现在变量后,先赋值,再对变量中保存的值进行自加1运算
		 *   ++出现在变量前,先进行自加1运算,再赋值
		 *   
		 *   --出现在变量后,先赋值,再对变量中保存的值进行自减1运算
		 *   --出现在变量前,先进行自减1运算,再赋值
		 */	 
		
		int a = 100;
		
 		int b = a++;//a = 101  ||  b = 100
		//int b = ++a;//a = 101  ||  b = 101

		//int b = a--;//a = 99  ||  b = 100
		//int b = --a;//a = 99  ||  b = 99
		
		System.out.println(a); 
		System.out.println(b); 
	}
}




public class Test03 {
    
    

	 public static void main(String[] args) {
    
    
		 
	        //++出现在变量后,先赋值,在对变量中保存的值进行自加1运算
	        int a = 100;
	        int b = a++;
	        System.out.println(a); //101
	        System.out.println(b); //100

	        //++出现在变量前,先进行自加1运算,在赋值
	        int m = 10;
	        int n = ++m;
	        System.out.println(m);//11
	        System.out.println(n);//11

	        int x = 500;
	        System.out.println(x++);//500
	        System.out.println(x);//501

	        int y = 100;
	        System.out.println(++y);//101
	        System.out.println(y);//101

	        //出现在变量后,先赋值,在对变量中保存的值进行自减1运算
	        int c = 100;
	        int d = c--;
	        System.out.println(c); //99
	        System.out.println(d); //100

	        //出现在变量前,先进行自减1运算,在赋值
	        int e = 10;
	        int f = --e;
	        System.out.println(e);//9
	        System.out.println(f);//9

	    }
}





public class Test04{
    
    
    public static void main(String[] args) {
    
    

		// 自增/自减运算
        int n = 666;
        n++; // 667, 相当于 n = n + 1;
        n--; // 666, 相当于 n = n - 1;
        int y = 100 + (++n); // n = 1 + n;即 100 + (1+666)
        System.out.println(y);
    }
}



》》》Expansion practice examples


public class Test05 {
    
    
	
	public static void main(String[] args) {
    
    
		// a 的值为多少?
		int a = 1;
		
		a+=4;
		//a-= 4;
		
		System.out.println(a);
	}
}




public class Test06 {
    
    
	
	public static void main(String[] args) {
    
    
		// a b 的值各为多少?
		int a = 1;
		
		int b = a++ + ++a;
		
		System.out.println(a);
		System.out.println(b);
	}
}

It can be seen from the above: a++ + ++a -> 2 + 1 -> a = 3 -> b = 1 + 3 -> b = 4




public class Test07 {
    
    
	
	public static void main(String[] args) {
    
    
		// a b c d的值各为多少?
		int a = 1;
		int b = 2;
		int c = 3;
		int d = a++ + ++b * c-- - ++a / b-- + ++c;
		
		System.out.println(a);//3
		System.out.println(b);//2
		System.out.println(c);//3
		System.out.println(d);//12
	}
}




public class Test08 {
    
    
	
		public static void main(String[] args){
    
    
			int a = 1;
			int b = a++;			// b = 1, a = 2
			int c = ++b;			// c = 2, b = 2
			int d = c--;			// d = 2, c = 1
			int e = --d;			// e = 1, d = 1
			
			//          2        2        2        0       1
			int f = a++ + b-- * ++c - --d / e++;
			
			System.out.println(a); // 3
			System.out.println(b); // 1
			System.out.println(c); // 2
			System.out.println(d); // 0
			System.out.println(e); // 2
			System.out.println(f); // 6
	}
}





  • Note that the calculation results are different when ++ is written before and after:
    ++n means adding 1 first and then referencing n; calculating first, then assigning
    n++ means citing n first and then adding 1; assigning first, and then
    calculating It is easy to confuse yourself by mixing it into routine calculations.

Guyu












Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2021 03 - Guyu.com | 【Copyright All Infringement Must Be Prosecuted】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/115163831