What about the auto increment and auto decrement operators (++ and ) in Java

  • 2021-06-29 11:01:26
  • OfStack

Self-increasing (++) and self-decreasing (--) operators

There are many operators in the JAVA language, but they may be seldom used in actual development and often appear in the first learning. Do you remember the meaning and usage of these operators?

1. Overview

The auto-increment operator (++) and the auto-decrement operator (--) are operations that add 1 to and subtract 1 from a variable.

2. Description of classification

++ and -- are abbreviated operators that increase and decrease variables by one.Many programming tasks often require variables to be added or subtracted by 1, so using these two operators is much easier.For example, the following code increases i by 1 and decreases j by 1:


int i = 3, j = 3;
i++; // i become 4
j--; // j become 2

i++ is read as i plus, i--as i minus.These operators are called post-auto-increment and post-auto-decrement operators, respectively, because the operators++ and--are placed behind variables.These operators can also precede variables, such as:


int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2

--i increases i by 1, --j subtracts j by 1.These operators are called pre-auto-increment and pre-auto-decrement operators.

As you can see, in the previous examples, i++ and ++ i have the same effect, or i--and--i have the same effect.However, when used in expressions that do not merely increase or decrease themselves, they produce different effects.Specifically, the following table describes the differences between the Auto-Increase and auto-decrease operators and gives examples:

操作符

名称

描述

示例(假设i=1)

++var

前置自增

将var加1,在语句中使用新的var值

Int j = ++i; // j is 2, i is 2

var++

后置自增

将var加1,但是在语句中使用原来的var值

Int j = i++; // j is 1, i is 2

--var

前置自减

将var减1,在语句中使用新的var值

Int j = --i; // j is 0, i is 0

var--

后置自减

将var减1,但是在语句中使用原来的var值

Int j = i--; // j is 1, i is 0


The following demonstrates supplementary examples of the pre-+ (or --) and post-+ (or --) forms.Consider the following code:


int i = 10;
int newNum = 10 * i++; // Left Equivalent to --- "  int newNum = 10 * i;
System.out.print("i is " + i // Connect strip  i = i + 1;
 + ", newNum is " + newNum);

Execute output:

i is 11, newNum is 100

In the above example, i increases by 1 and then returns the original value of i to participate in the multiplication operation.Thus, the value of newNum is 100.Replace i++ with +i+:


int i = 10;
int newNum = 10 * (++i); // Left Equivalent to --- "  i = i + 1;
System.out.print("i is " + i // Connect strip  int newNum = 10 * i;
 + ", newNum is " + newNum);

Execute output:

i is 11, newNum is 110

In the above, i adds itself 1, then returns the new value of i and participates in the multiplication operation.Thus, the value of newNum is 110.

Here is another example:


double x = 1.0;
double y = 5.0;
double z = x-- + (++y);

After these three lines are executed, the value of y is 6.0, z is 7.0, and x is 0.0.

Evaluate operands from left to right in Java.Before any part of the right operand is evaluated, the operand to the left of the 2-ary operator is evaluated.This rule takes precedence over any other rule of the expression.Here is an example:


int i = 1;
int k = ++i + i * 3;
//  Analysis shows that:  ++i Return after evaluation 2 .On demand i*3 When the value of i yes 2 .therefore k The value of is 8 . 

//------------ Compare the following codes ------------------
int i = 1;
int k = i*3 + ++i ;
//  Analysis shows that from left to right, demand i*3 When the value of i yes 1 After calculation ++i , ++i The whole is 2 .therefore k The value of is 5 . 

3. Summary

The self-increasing Java written tests and interviews often appear as the basic questions for examining programmers many times, which is also a more easily confused point of knowledge.Using self-increasing and self-decreasing operators in normal programming can make expressions shorter, but it can also make them more complex and difficult to understand.You should avoid using these operators to modify multiple variables in the same expression or the same variable multiple times, such as int k = ++i + i * 3.

4. Description

This paper is abstracted from the content of chapter 2.14 of the 11th edition of Java Language Programming and Data Structure (Foundation). It combines with examples and supplements and extends some sample codes. This paper is used for my learning and summary of experience.


Related articles: