Home  >  Article  >  Java  >  The difference between i++ and ++i in java

The difference between i++ and ++i in java

王林
王林Original
2019-11-21 10:45:133801browse

The difference between i++ and ++i in java

#i means assigning the value first and then calculating, i means calculating first and then assigning the value.

The example is as follows:

package com.test;
 
/**
 * @author Administrator
 * @date 2018/6/9
 */
public class TestAdd {
 
    public static void main(String[] args) {
        int a = 0;
        int b = a++;
        int c = ++a;
        System.out.println("a:" + a);
        System.out.println("b:" + b);
        System.out.println("c:" + c);
    }
 
}

int b = a ; means first assign the value of a to b, and then calculate 1.

int c = a; means first calculating the value of a, 1, and then assigning the value of a to c.

If there is no variable to receive a or the value of a, simply using these two operations will make no difference in the result.

The same is true for a-- and--a.

Recommended tutorial: java quick start

The above is the detailed content of The difference between i++ and ++i in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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