Like C, Java provides a rich shortcut for computing. These shortcuts can make the code more refreshing, easier to input, and easier for readers to read.
Two good shortcuts are the increment and decrement operators (often referred to as "AutoIncrement" and "auto decrement" operators). Where the decrement operator is "--", meaning "reduce one unit"; The increment operator is "+ +", meaning "add one unit". For example, assuming a is an int (integer) value, the expression ++a is equivalent to (a = a + 1). The increment and decrement operators result in the value of the variable.
There are two versions available for each type of operator, often referred to as "prefix version" and "suffix version." Pre-increment indicates that the + + operator is in front of a variable or expression, and "after increment" indicates that the + + operator is behind a variable or an expression. Similarly, "forward descending" means that the operator is in front of a variable or expression, and "after descending" means-the operator is behind a variable or expression. For forward increment and forward decrement (such as ++a or--a), the operation is performed first, and then the value is generated. And for after-increment and decrement (such as a++ or a--), Mr will be a value, and then perform the operation. Here is an example:
: Autoinc.java
//demonstrates the + + and--operators public
class Autoinc {public
static void main (String [] args {
int i = 1;
PRT ("I:" + i);
PRT ("++i:" + ++i); Pre-increment
prt ("i++:" + i++); Post-increment
prt ("I:" + i);
PRT ("I:" +-I.); Pre-decrement
prt ("i--:" + i--); Post-decrement
prt ("I:" + i);
}
static void Prt (String s) {
System.out.println (s);
}
}///:~
The output of the program is as follows:
I:1
++i:2
i++: 2
i:3
--i:2 i--
: 2
i:1
As you can see, for the prefix form, we do not get the value until the operation is done. For the suffix form, however, the value is obtained before the operation is executed. They are the only operators with "side effects" (except those that involve assignment). That is, they change the arithmetic objects, not just their own values.
The increment operator is an explanation of the name "C + +", implying "one step of overloading C". In an earlier Java speech, Bill Joy (one of the founding people) claimed that "java=c++--" (C Gaga minus) meant that Java had gone beyond C + + to create a leaner language. As you will learn in this book, many parts of Java have been simplified, so Java learning is easier than C + +.