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

后端 未结 8 848
梦毁少年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:30

    It is the unary plus, twice. It is not a prefix increment because there is a space. Java does consider whitespace under many circumstances.

    The unary plus basically does nothing, it just promotes the operand.

    For example, this doesn't compile, because the unary plus causes the byte to be promoted to int:

    byte b = 0;
    b = +b; // doesn't compile
    

提交回复
热议问题