Glenn
Glenn

Reputation: 12809

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

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?

Upvotes: 31

Views: 4167

Answers (8)

mrjoltcola
mrjoltcola

Reputation: 20842

So everyone has said that it is a unary plus that promotes its operand to an integer value. I noticed a few comments about how Java allows useless constructs. Well, so does every other language, including formal mathematics.

This is not a Java specific thing. It is an impossible task for compiler writers to try to "predict" every useless thing a programmer could potentially write within a grammar, and try to restrict those by adding semantic checks in the compiler. Plus, "useless" is a subjective term. One man's useless is another man's learning tool. You leave that up to the programmer. If he wants to write useless code, so be it. As long as it produces correct code, we should be satisfied.

Code is like math. It may be useful, or not. But it just is, just like the number 42. It is neither useful or useless, depending on context. It is just 42.

What we don't need is any extra semantic code within the Java compiler to restrict programmers. There is enough bondage in the language already.

Upvotes: 3

HJK
HJK

Reputation: 1382

In this case, Java considers the + operator as a unary operator and hence the result is 10. Just try with the following code:

int e = + - 10; // result is -10
int e = - - 10; // result is 10

Upvotes: 34

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

The +'es are the unary plus operator, indicating sign of the value to the right of it, not the addition operator used when adding two values.

Consider this line

int e = + + + + + + + + 10;

which is equivalent to this

int e = +( +( +( +( +( +( +( +10)))))));

which again is equivalent to this

int e = +10;

and is similar (in form) to

int e = -10;

which probably is more straightforwardly understandable.

And by the way, + + is not equivalent, nor evaluated by the parser as the increment operator ++!

Upvotes: 28

Bathsheba
Bathsheba

Reputation: 234655

++ and + are both operators. They are elements of the language. Technically they can be referred to as tokens. + + is not a single token in Java and is broken down into two individual tokens during the parsing stages of compilation.

+ can exist in two forms: (i) as a unary operator; for example +10 (which is essentially a no-op which only adds to the confusion) or (ii) as a binary operator (meaning it acts on two thing) to add two terms; for example 10 + 10.

Since they have an intrinsic value, you can also regard 10 and +10 as both being expressions. You should also note that the unary + has a very high operator precedence so it will bind tightly to the expression immediately after it.

So what is happening in your code is the the compiler is binding the + just to the left of the expression 10 to produce another expression with value 10. Then that binds the second leftmost unary + to produce, yet again, 10.

In summary: without the space, ++ is a single token. With any space between them, the two + act as two unary operators.

Upvotes: 21

Tarik
Tarik

Reputation: 11209

10 is a literal not a variable. As such, 10 cannot be incremented.

++somevariable increments somevariable.

++variable -> increment operator.

+ + someliteral will just equate to someliteral.

+ + literal -> successive unary + operator

Upvotes: 2

fluminis
fluminis

Reputation: 4039

Do not confound the preincrement operator ++ and the unary operator + repeated twice.

int e = ++10; // won't compile "invalid argument to operation ++/--"
int e = + +10; //compile and e equals to 10

Upvotes: 5

Radiodef
Radiodef

Reputation: 37845

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

Upvotes: 60

M A
M A

Reputation: 72844

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

Upvotes: 5

Related Questions