Why does this Java code with “+ +” compile?

后端 未结 8 855
梦毁少年i
梦毁少年i 2020-12-10 10:25

I\'m curious why this simple program could be compiled by java using IntelliJ (Java 7).

public class Main {
    public static void main(String[] args)
    {
         


        
8条回答
  •  长情又很酷
    2020-12-10 11:20

    The +'es are the unary plus operator, indicating sign of the value to the right of it, not the addition operator used when adding two values.

    Consider this line

    int e = + + + + + + + + 10;
    

    which is equivalent to this

    int e = +( +( +( +( +( +( +( +10)))))));
    

    which again is equivalent to this

    int e = +10;
    

    and is similar (in form) to

    int e = -10;
    

    which probably is more straightforwardly understandable.

    And by the way, + + is not equivalent, nor evaluated by the parser as the increment operator ++!

提交回复
热议问题