【发布时间】:2015-08-09 07:22:37
【问题描述】:
我想知道post 和pre 的递增和递减操作。
我在Java 中知道post operator 的优先级是high 和associativity 是left-to-right。而pre 运算符的关联性是right-to-left
但我的代码显示了不想要的结果-
public class Bunnies {
static int count = 3;
public static void main(String[] args) {
System.out.println(--count*count++*count++);//out put is 12 expected 48
//associativity of post is higher so should be evaluated like this-
//--count*3**count++ count is 4 now
//--count*3*4 count is 5 now
//4*3*4=48
count = 3;
System.out.println(--count*++count*++count); //out put is 24 expected 120
//associativity of pre is right to left so should be evaluated like this-
//--count*++count*4 count is 4 now
//--count*5*4 count is 5 now
//4*5*4=120
count = 3;
System.out.println(-- count*count++);// out put is 4 expected 9
//--count*3 count is 4 now
//3*3=9
}
}
【问题讨论】:
-
我正在检查这些
post和pre是如何工作的。不是实时应用程序的一部分。我在C中很正常,这些操作员在 java 中购买时想知道我。 -
您应该信任并引用 Java 语言规范,而不是任何其他文档,包括 Java 教程。
-
当你有 --Number 时,数字会先减少然后执行下一个操作。当你有 Number++ 时,就会执行操作,然后 Number 会增加
-
图是C++,不是Java...
-
@ِAllloush我正在关注oracle文档docs.oracle.com/javase/tutorial/java/nutsandbolts/…
标签: java operators operator-precedence associativity