簡體   English   中英

如果在 JAVA 中后遞減的優先級高於前遞增,那么為什么以下代碼 output 為 22.0 而不是 20.0。

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM