Reputation: 737
public static void main(String[] args) {
int x = 1 + + + + + + + + + 2;
System.out.println(x);
}
I can compile above method. Is there any explanation about the allowed multiple "+" operator?
Upvotes: 6
Views: 208
Reputation: 52
Its because though syntactically it may seem wrong use of '+' but there is this unary operation is repeating itself.
Upvotes: 0
Reputation: 284786
It's addition, then the unary plus operator repeated. It's equivalent to the following:
int x = 1 + (+ (+ (+ (+ (+ (+ (+ (+ 2))))))));
Upvotes: 11
Reputation: 27199
you do not get any exceptions, it works fine. You will get output 3.
Upvotes: 0
Reputation: 18741
I think they treated all those plus as the same one +. Because the output is 3, so there is no magic here at all
Upvotes: 0