简体   繁体   English

Java算术运算符,请解释输出

[英]Java arithmetic operators ,Please explain the output

public class MainClass
{

     public static void main(String[] args)
     {
        int i = 13 - - 14 + + 15;
        // evaluate this as Right to Left associativity 
        // so (13 - (-14)) + + 15 = 27 + +15 = 42 --> correct 
        System.out.println(i);
     }
}

Since + and - has same precedence , so it will have right to left associativity. 由于+和-具有相同的优先级,因此它将具有从右到左的关联性。 So I thought that any expression like 13 - - 14 + + 15 will be considered as 13 - (-14 + + 15) and output will be 12, but output is coming 42. Can anyone please explain the output? 因此,我认为任何类似13--14 + + 15的表达式都将被视为13-(-14 + + 15),输出将为12,但输出即将到来42。有人可以解释一下输出吗?

您可以看到13 - - 14 + + 15就像13 - (-14) ,就像13 + 14等于27 ,其次是+ (+ 15)就像+ 15等于42。

如果从左至右(如Java一样)计算表达式,你下手13 - - 14这是一样的13 - (-14)这是27 27+ +15是42。

从左到右评估为13-(-14)+(+15),因此13 + 14 + 15 = 42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM