[英]If post decrement have higher precedence than pre increment in JAVA then why does the following code output as 22.0 and not as 20.0 .?
根據優先規則,首先應評估quantity--
- 應評估應評估為10
然后應評估++quantity
應評估應評估為10
然后執行加法,結果應為20.0
。
int quantity = 10;
double total = ++quantity + quantity--;
System.out.println("total is: - " + total);
優先規則告訴您如何解析表達式。 具有更高優先級的后遞減意味着++quantity + quantity--
必須解析為
((++quantity) + (quantity--))
而不是例如下面這甚至沒有意義
((++quantity) + quantity)--
評估順序不受影響。 表達式仍然從左到右進行計算。 即,+ 的左分支在右分支之前。 這意味着++quantity
必須在quantity--
之前進行評估。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.