31

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?

7

8 Answers 8

60

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
6
  • 1
    @SargeBorsch I wouldn't say it's useless. You can write +10 in a math equation and it's not wrong. You can also write ++10 (a positive positive ten), but in programming ++ is overloaded to mean increment, and isn't signifying the sign, so the equivalent would be + +. It says more about the programmer/mathematician using redundant sign indicators than it does about some design deficit in Java.
    – Shaz
    Commented Jun 18, 2014 at 20:10
  • 6
    @Ryan ++ is clearly not equivalent to + +. "Not wrong" doesn't imply "useful". Also, in math nobody uses unary + with numbers, only as a shorthand notation for "x approaches X from positive side" when defining a limit. (for example, x → +0) Because redundancy is not cool in mathematics, too. But Java coders love redundancy... Commented Jun 18, 2014 at 20:37
  • 1
    @SargeBorsch My point was that ++ in mathematics is similar to + + in programming. It looks weird but it's not wrong to do it. Also, this works in C++ and C# so it's not just a Java thing. It's fine if you don't like Java but this isn't a solid battleground against it. It's simply a by-product of allowing the unary + operator, which is just as valid as the unary -.
    – Shaz
    Commented Jun 18, 2014 at 20:50
  • Why would anyone write something excessive only because it's "not wrong" and at the same time not improving clarity? Writing totally unneeded code is polluting code. Hovewer, maybe it works when coders are paid for amouts of typed chars... Reminds me a famous trick "if (x == true) ..." haskell.org/haskellwiki/Unary_operator This is a good example how a language should be done :) Also, to be honest, I like Java more than C# and of course C++, because C# and C++ have even more dust which is useless and non-orthogonal, or even harmful. Commented Jun 18, 2014 at 21:10
  • 1
    @Radiodef Aligning is questionable thing, too... for example, an IDE formatter may destroy it. And when you change numbers, you would need to align again. And also VCS issues... Unless you use some very special tools for that, it's a big waste of time for a questionable profit. Commented Jun 18, 2014 at 21:22
34

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
1
  • 1
    @NimChimpsky Because the prefix increment is ++. Spaces matter.
    – awksp
    Commented Jun 18, 2014 at 7:20
28

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 ++!

1
  • 1
    Nice to see the clarifying examples.
    – Almo
    Commented Jun 18, 2014 at 16:57
21

++ 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.

5

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

5

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
3

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.

2

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
5
  • 4
    ++10 won't compile here.
    – PeterMmm
    Commented Jun 18, 2014 at 7:17
  • The variable didn't incremented
    – Glenn
    Commented Jun 18, 2014 at 7:23
  • Guys, give me a break. I am struggling with the editor.
    – Tarik
    Commented Jun 18, 2014 at 7:25
  • 2
    @Glenn 10 is a literal not a variable. As such, 10 cannot be incremented.
    – Tarik
    Commented Jun 18, 2014 at 7:28
  • 1
    Average Stack Overflow downvote trigger finger response time = 0.7ms +/- 0.2ms for network latency
    – mrjoltcola
    Commented Jun 19, 2014 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.