java中a++和++a或a--和--a有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20610769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:06:36  来源:igfitidea点击:

what is the difference between a++ and ++a or a-- and --a in java?

java

提问by Ghadeer

 public void push(E element) {
    if (size == elements.length) {
        resize(); // doubel of size
    }
    elements[size++] = element;
}


public E pop() {
    if (size == 0) {
        throw new java.util.EmptyStackException();
    }
    E element = elements[--size];
    elements[size] = null; // set null in last top
    return element;
}

what is the difference between a++ and ++a or a-- and --a in java

java中a++和++a或a--和--a有什么区别

thanks

谢谢

采纳答案by Denim Datta

a++or a--is postfix operation, meaning that the value of a will get changed after the evaluation of expression.

a++ora--是后缀操作,意思是 a 的值会在表达式求值后改变。

++aor --ais prefix operation, meaning that the value of a will get changed before the evaluation of expression.

++aor--a是前缀操作,意味着 a 的值将在表达式计算之前发生变化。

lets assume this;

让我们假设这一点;

a = 4;

b = a++; // first b will be 4, and after this a will be 5

// now a value is 5
c = ++a; // first a will be 6, then 6 will be assigned to c

Refer this answeralso.

也参考这个答案