Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Java code with "+ +" compile?

Tags:

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?

like image 613
Glenn Avatar asked Jun 18 '14 07:06

Glenn


People also ask

Why is Java code compiled?

Java code needs to be compiled twice in order to be executed: Java programs need to be compiled to bytecode. When the bytecode is run, it needs to be converted to machine code.

How do you fix a compilation error in Java?

To make matters worse, the associated Java compile error poorly describes the source of the problem. To fix the problem, simply make the "I" in "Integer" lowercase and make the "s" in "system" uppercase: int x = 10; System.

Why is Java both compiled and interpreted?

The Java source code first compiled into a binary byte code using Java compiler, then this byte code runs on the JVM (Java Virtual Machine), which is a software based interpreter. So Java is considered as both interpreted and compiled. The compiled byte code allows JVM to be small and efficient, and fast performing.


1 Answers

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 
like image 152
Radiodef Avatar answered Oct 03 '22 21:10

Radiodef