3

I have searched the web and it gives me a divide answer. Some said it is only an unary operator and some said it is an arithmetic operator. Thanks

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Ko Ye
  • 77
  • 2

2 Answers2

3

These classifications don't contradict each other.

++ is an unary operator, as it applies to a single operand.

++ is also an arithmetic operator, as it applies to a number and performs an arithmetic operation (incrementing the number by one).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

I think it won't be a mistake to call i++ both Arithmetic and Unary Operator.

However, Oracle doc here distinguishes and classifies ++ as Unary operator.

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • I can't add anything to the answer. It's just a plain answer to the question asked plainly. Not to say much words, if we dive into [§15.14.2 – 15.15](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.14.2) of Oracle Docs, indeed there is a separation between _Postfix_ Increment Operator ++ and Unary Operators. But as the question was about unary _or arithmetic_, my worring of it could be exessive. – HoRn Feb 01 '20 at 12:13
  • Then, at last, at the beginning of [§15.15](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.15) it's said (in general?): "The operators +, -, ++, --, ~, ! ... are called the unary operators". The term "unary" then seems not so strict to argue about. Then i++ is also arithmetic because, as Mureinik said, simply, it performs an arithmetic operation – HoRn Feb 01 '20 at 12:14