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

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

    It's just the unary plus. If you compile int e = + + 10 you will get the following bytecode:

    bipush 10
    istore_1
    

    which is exactly equivalent to int e = 10

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题