Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you remove the space between the + and ++ operators?

EDIT 1

DISCLAIMER: I know that +++ is not really an operator but the + and ++ operators without a space. I also know that there's no reason to use this; this question is just out of curiosity.


So, I'm interested to see if the space between + and ++var is required in Java.

Here is my test code:

int i = 0;
System.out.println(i);
i = i +++i;
System.out.println(i);  

This prints out:

0
1

which works as I would expect, just as if there were a space between the first and second +.

Then, I tried it with string concatenation:

String s1 = "s " + ++i;
System.out.println(s1);
// String s2 = "s " +++i;

This prints out:

s 2

But if the third line is uncommented, the code does not compile, with the error:

Problem3.java:13: unexpected type
required: variable
found   : value
    String s2 = "s " +++i;
                ^
Problem3.java:13: operator + cannot be applied to <any>,int
    String s2 = "s " +++i;
                     ^

What's causing the difference in behavior between string concatenation and integer addition?


EDIT 2

As discussed in Abhijit's follow-up question, the rule that people have mentioned (the larger token ++ be parsed first, before the shorter token ++) is discussed in this presentation where it appears to be called the Munchy Munchy rule.

like image 550
chm Avatar asked Mar 13 '13 06:03

chm


People also ask

What happens when you delete a connector space?

First and foremost, if you are about to delete a connector space and have synchronization rules in your environment, you have to disable synchronization rule provisioning. Failure to do so can (and usually does) result in duplicate or orphaned metaverse objects.

What are the new and delete operators in C++?

C++ supports these functions and also has two operators new and delete that perform the task of allocating and freeing the memory in a better and easier way. This article is all about new and delete operators. The new operator denotes a request for memory allocation on the Heap.

How do I fix a space between two lines in word?

To fix that: Right-click the text of the line just above the space and click Paragraph. In the Paragraph dialog, make sure the Space Below box contains a zero. Click OK. Repeat for the line just below the space, and make sure the Space Above box contains a zero. Click OK. Repeat all of this for each incorrect space.

How to remove the space between parent paragraph and child paragraph?

So, for example if this were directly on two adjacent paragraphs each paragraph (yes you would have to do this twice) would be: Setting padding to 0px of the parent paragraph tag will remove the space between parent and child paragraphs if one is inside of another.


1 Answers

There is no +++ operator. What you have there is a postfix ++ operator followed by an infix + operator. That is a compilation error because postfix ++ can only be applied to a variable, and "s " isn't a variable.

Since you really mean an infix + operator followed by a prefix ++ operator, you need to put the space in between the operators.

Actually, you should do it ANYWAY. +++ is a crime against readability!!!

like image 186
Stephen C Avatar answered Sep 20 '22 12:09

Stephen C