-5

For the following program is it necessary to have the brackets in the statement of post-increment or decrement

public class sample
{
  public void f1()
  {
    int x=4;
    x+=(x++)+(++x);
System.out.println(x);
  }
}

^^Is this equal to

public class sample
{
  public void f1()
  {
    int x=4;
    x+= x++ + ++x;
System.out.println(x);
  }
}

bhargav
  • 1
  • 2

3 Answers3

1

It is not necessary to use brackets. Both prefix and postfix ++ have a higher precedence than +.

However, it is necessary to use something to separate the ++ and + operators. If you don't separate them (with whitespace, comments or other tokens) you get a rather confused compilation error. For example:

Test.java:3: error: unexpected type
    int j = i +++++ i;
              ^
  required: variable
  found:    value
1 error

Here's what is going on:

  1. The lexical analyser reads the above as int j = i ++ ++ + i ;. This is a consequence of JLS 3.2:

    "The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would."

  2. Taking precedence into account, the parser parses the expression as:

                 +
                / \
              ++   i
              /
            ++
            /
           i
    

    where ++ is the postfix increment operator. That is equivalent to ((i++)++) + i.

  3. The analyser detects that postfix ++ is being applied to the result of i++, which is a value. That is illegal. The operand of ++ has to be variable not a value.

    As JLS 15.14.2 says:

    "The result of the [operand] must be a variable of a type that is convertible to a numeric type, or a compile-time error occurs."


But seriously, don't write code like this. It is just confusing ... and unnecessary.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • It tokenizes as `i ++ ++ + i` and parses as `((i++)++)+i`. `i++` is the unexpected value which can't be (re-)incremented. – Sneftel Mar 16 '19 at 09:27
  • Ah yes, I see. So it is tokenizing, but the heuristic the JLS has specified gives a token sequence that doesn't make sense to the analyser. The inner `(i++)` needs to be an *lvalue*, not just any old expression. – Stephen C Mar 16 '19 at 09:30
0

There’s no need for parentheses but you do need to separate the +s.

Java uses maximal munch when parsing. That means it consumes as much of the text as it can to make tokens.

So, for example,

+++++

is parsed as

++ ++ +

and that makes no sense (the post increment result is not an l-value) so the compiler issues a diagnostic.

With ++ + ++ the normal grouping rules apply (informally speaking, operator precedence), so the entire expression is grouped as you have it in the first snippet you present.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You should not wrote code like this. I cannot think of any use of this and it is confusing for the reader.

The answer can be found easily by writing some test code, but it does not matter.

Donat
  • 4,157
  • 3
  • 11
  • 26