1

Could anyone tell me why I get a compiler error here?

class NewInteger{
  public static void main(String[] args){
    Integer i = new Integer(200);
    Integer j = ++i;                    //ok
    Integer k = ++(new Integer(300));   //compile error: unexpected type
                                        //               required: variable
                                        //               found: value
  }
}

Seems to me the offending line is just a terser version of the two preceeding it.

twisted
  • 742
  • 2
  • 11
  • 19
  • 1
    Closely related http://stackoverflow.com/questions/15291861/why-doesnt-the-post-increment-operator-work-on-a-method-that-returns-an-int – Tunaki Aug 14 '16 at 11:58

4 Answers4

8

++ reads from a variable, increments the value read, and writes back to the variable. new Integer(300) is not a variable, you can't write to it.

That's the critical difference between your two examples: In the j = ++i case, ++ has something it can write back to (i). In the second case, it doesn't.


(Using "variable" here the way the JLS frequently [though oddly not always] does, to mean both local variable and instance or class field.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Seems to me the offending line is just a terser version of the two preceeding it.

That's not actually true. The ++ (in both, its prefix as well as its suffix version) has side-effects on its operand, which requires, that the operand is a variable and not just some expression. Disregarding the prefix/postfix distinction,

Integer j = ++i;

is actually something like

Integer j = (i += 1);  // Note the side-effect on i

in spirit.

To see the side effect happen, try printing the value of i before your second line, i.e.,

System.out.println(i);
Integer j = ++i;
System.out.println(i);

and observe, that the values printed are different.

Dirk
  • 30,623
  • 8
  • 82
  • 102
1

In languages derived from C (like Java) there are two types of expressions: lvalues and rvalues. Lvalues are ones that can appear on either side of an assignment expression and rvalues can only appear on the right. Generally lvalues are names of variables, which means they have a specific location in memory to back them. This makes sense because of course when you do an assignment the expression on the left needs to have a location to store the result. So for example the expression a is the name of a variable and so it can be an lvalue. But a + 1 just calculates a temporary result and so there is nowhere to store the value if it were used as an assignment location.

The ++ operator needs an lvalue because as well as adding one to the expression it tries to store the result in the variable being incremented. In the first example this works because i is an lvalue and it can store the result of the calculation in the memory for the variable. In the second example (new Integer(300)) is an rvalue because it is just a temporary calculation with no location to store the result. Therefore it can not be used with the ++ operator.

Neil Roberts
  • 469
  • 3
  • 11
0

you can increment only variable. on first case

Integer j = ++i;

you have incremented a varibale and assigned to Integer type j.

and in second case ,you wanted to increment a class Integer which is not possible

 Integer k = ++(new Integer(300));
jitendra varshney
  • 3,484
  • 1
  • 21
  • 31