Home > Java > javaTutorial > How to use increment and decrement operators in Java

How to use increment and decrement operators in Java

WBOY
Release: 2023-04-20 18:22:08
forward
1792 people have browsed it

Explanation

1. The self-increment () self-decrement (-) operator is a special arithmetic operator. It requires two operands to operate, and the self-increment The self-reduce operator is one operand.

2. Prefix auto-increment and auto-decrement method (a, -a): perform self-increment or self-decrement operation, and then perform expression operation.

3. Suffix auto-increment and self-decrement method (a, a-): perform expression operation first, and then perform auto-increment or self-decrement operation.

Example

public static void main(String[] args) {
    int a = 5;//定义一个变量;
    int b = 5;
    int x = 2 * ++a;
    int y = 2 * b++;
    //自增运算符前缀运算后a=6,x=12
    System.out.println("自增运算符前缀运算后a=" + a + ",x=" + x);
    //自增运算符后缀运算后b=6,y=10
    System.out.println("自增运算符后缀运算后b=" + b + ",y=" + y);
}
Copy after login

What are the characteristics of Java

1.As a representative of static object-oriented programming language, Java language realizes object-oriented theory, Allows programmers to perform complex programming with elegant thinking.

2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, and dynamic nature.

3. Use Java to write desktop applications, Web applications, distributed systems and embedded system applications, etc.

The above is the detailed content of How to use increment and decrement operators in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template