Java Compilation – Why Does This Java Code with ‘++’ Compile?

java

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)
    {
        int e = + + 10;
        System.out.println(e);
    }
}

The output is still 10. What is the meaning of + + 10?

Best Answer

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
Related Question