0

I don't know what happens if I apply the increment operator on an expression in Java.

int ai[] = new ai[10];
ai[0]++;

// ***

class Car {
  public int yearMade = 0;
}

class Person {
  public Car myCar = new Car();
}

Person p = new Person();
p.myCar.yearMade++;

Can you increment an element of an array the way the first example is showing?

Can you increment a field in a class (I do know about encapsulation and getters/setters, my question is syntax-semantics oriented) the way I show in the second example?

I remember the age of C/C++. There used to be a problem with p -> x++, for example. You sometimes needed to enclose complex expressions in parentheses when using increment or decrement.

Thank you for any hints.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Pavel Foltyn
  • 175
  • 3
  • 13
  • I spent about an hour looking for answers to my questions. But people are interested in different issues regarding increment / decrement. Prefix-postfix, for example. Nothing about operator precedence, parenthesizing etc. – Pavel Foltyn Sep 09 '19 at 14:59
  • Your question was if this can be done. You even have the code. Just run it and see :D – f1sh Sep 09 '19 at 15:01
  • Nope. I need a systematic answer, not a result of a random experiment or two. I did not find it at oracle.com either. If you get me a link to documentation I'll stop bothering stackoverflow users. – Pavel Foltyn Sep 09 '19 at 15:04
  • Thank you anyway, @f1sh, for commenting on my question. – Pavel Foltyn Sep 09 '19 at 15:13
  • No problem. Both of these examples are possible. :) – f1sh Sep 09 '19 at 15:15
  • 1
    https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.26 – Johannes Kuhn Sep 09 '19 at 15:15

1 Answers1

1

The answer to both your questions is "Yes, you can". Both p.myCar.yearMade and ai[0] are variables (an instance variable and a local variable, respectively), and, thus, can be used as operands for any of these four operators.

4.12. Variables

A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type (§4.2) or a reference type (§4.3).

A variable's value is changed by an assignment (§15.26) or by a prefix or postfix ++ (increment) or -- (decrement) operator (§15.14.2, §15.14.3, §15.15.1, §15.15.2).

...

15.14.2. Postfix Increment Operator ++

At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • Thank you. This is what I was after. – Pavel Foltyn Sep 09 '19 at 15:54
  • May I have two more premium questions? `class A { public Integer num = 0; public Integer getNum() { return num; } } A a = new A(); a.num++; a.getNum()++; ` The second one is probably a nonsense. On the other hand, the getter returns an object (Integer), so you have reference to that object, so theoretically you could increment the instance field using its getter, couldn't you? – Pavel Foltyn Sep 09 '19 at 16:01
  • 1
    @PavelFoltyn it's the "nonsense" I [asked](https://stackoverflow.com/questions/36165743/variable-expected) a while ago :) What if `a.getNum()` returned an `int` constant, e.g. `42`? The compiler has no idea if it's going to be a generated value or a variable. It should know exactly that it's a variable (a storage location) so it can put the result from the operator back to that storage. Otherwise, where to put the result? – Andrew Tobilko Sep 09 '19 at 16:37
  • 1
    @PavelFoltyn the general CS term is probably [L-value](https://en.wikipedia.org/wiki/L-value) – Andrew Tobilko Sep 09 '19 at 16:39
  • @PavelFoltyn the object returned by `getNum()` is of type `Integer`, which is immutable. You can’t increment an `Integer` object and there’s no operator overloading to redirect an increment operator to some property. If you can dereference the returned object to a variable, however, you can increment the variable, e.g. `methodReturningYourA().num++` works and likewise, the expression `numericArrayReturningMethod()[index]++` would work. – Holger Sep 17 '19 at 13:39