Compiler doesn't complain when I ended a line with two semicolons. Why?

后端 未结 7 2232
不思量自难忘°
不思量自难忘° 2021-01-19 01:29

I thought bad thing would happen when I ended a line like this. But compiler didn\'t even complain. Does anybody have an idea, why this is legal in java.

displ

7条回答
  •  时光取名叫无心
    2021-01-19 02:00

    Here's a practical example in C:

    #ifndef NDEBUG
    #define Dprintf printf
    #else
    #define Dprintf(X)
    #endif
    
    if(some_test)
       Dprintf("It's true!");
    else
       Dprintf("It's false.");
    

    If NDEBUG is defined, the compiler will see:

    if(some_test)
       ;
    else
       ;
    

    This can be useful in some cases.

    Since Java is heavily inspired by C/C++, it's something they kept, even though it is not useful and there is no preprocessor.

    You can, if you want, run the C preprocessor cpp on your code to do this kind of behaviour.

    Some languages may say it's an error instead, it's up to them to choose.

提交回复
热议问题