Home > Java > Javagetting Started > The difference between i++ and ++i in java

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

王林
Release: 2019-11-21 10:45:13
Original
3888 people have browsed it

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);
    }
 
}
Copy after login

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!

Related labels:
source:php.cn
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