Java: Difference between +--i and +++i
<p>Last day while programming, I mistakenly wrote something like this-</p>
<pre><code>int i = 2;
int j = 3;
int a = i+++j;
</code></pre>
<p>And it did not shoot any error and I got-</p>
<blockquote>
<p>a = 5</p>
</blockquote>
<p>After detecting this coding error I was curious. So, I started playing with it. When I changed it a little-</p>
<pre><code>int a = i+ ++j;
</code></pre>
<p>I got-</p>
<blockquote>
<p>a = 6</p>
</blockquote>
<p>With this-</p>
<pre><code>int a = i+ + +j;
</code></pre>
<p>and this-</p>
<pre><code>int a = i++ +j;
</code></pre>
<p>I again got-</p>
<blockquote>
<p>a = 5</p>
</blockquote>
<p><strong>Similar situation was experienced <a href="https://stackoverflow.com/questions/18039908/weird-java-syntax">here</a></strong>.</p>
<p>But here comes the weird part. There is no difference between <code>a = i+--j</code> &amp; <code>a = i+ --j</code>. Both gives-</p>
<blockquote>
<p>a = 4</p>
</blockquote>
<p>Why is that? I completely understand what exactly is happening here. The thing I do not understand is- <strong>'WHY?'</strong>. <code>+</code> and <code>-</code> are both operators, then why there is a difference?</p>
<hr />
<p>For clarity, I wish to share another odd experience. This code works perfectly-</p>
<pre><code>int i=0;
System.out.println(&quot;value: &quot;+--i);
</code></pre>
<p>And outputs-</p>
<blockquote>
<p>value: -1</p>
</blockquote>
<p>But this-</p>
<pre><code>System.out.println(&quot;value: &quot;+++i);
</code></pre>
<p>Gives following error-</p>
<blockquote>
<p>error: unexpected type</p>
</blockquote>