Automatic increment and decrement operators for Java

Source: Internet
Author: User
Tags expression

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 + +.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.