使用 -- 运算符传递参数时会发生什么?

What happens when a parameter is passed with the -- operator?

我在 Java 中遇到了以下代码:

public static void foo (int a, int b) {
    System.out.println("a: " + a);
    System.out.println("b: " + b);
}

public static void main(String[] args) {
    int c = 1;
    foo(c--, c);
    System.out.println("c: " + c);
//The output is:
//a: 1
//b: 0
//c: 0

谁能解释一下,这是为什么?我会反过来猜到只有第一个打印语句会受到 c-- 的影响。但如果它确实永久改变了 c 的值,为什么第一行打印仍然打印 1?

可以用++cc++递增,同样可以用--cc--

递减

对于 ++c--c,你实际上是在告诉 Java "Change the value, then do something with the new value"

对于 c++c--,你告诉 java "Load the value, then change it"。

现在,这就解释了为什么 A 是 1,C 是 0。但是为什么 B 也是 0?

调用函数时,参数会按顺序解析。通常这仅在您将方法的直接结果作为参数传递时才有意义。

例如,调用 methodX( methodY(), methodZ()) 方法签名在哪里 void methodX(int a, int b); int methodY();int methodZ();,则方法 Y 将在 Z 之前执行,Z 在 X 之前执行。