is instanceof an operator?
Gavin Bierman
gavin.bierman at oracle.com
Mon Nov 25 18:15:11 UTC 2024
Hi Anton,
Thanks for your email. It is perhaps a little confusing, but chapter 3 is talking about the lexical structure of the language, i.e. the tokens. So, 3.12 is talking about the syntactic tokens that we call the operators. (Perhaps more precisely, the operator tokens.)
Chapter 15, in contrast, is talking about the various categories of expressions. It uses “operator” in a quite different sense (really it is talking about operator expressions). For example in 15.5, when talking about errors in casts:
In a cast, when the actual class of the object referenced by the value of the
operand expression is not compatible with the target type specified by the cast
operator (§5.5, §15.16); in this case a ClassCastException is thrown.
Note the reference to the cast operator here (in bold), which isn’t in the list of 3.12 either.
So, when reading chapter 15, one should have this categorisation in mind (from 15.2):
Expressions can be broadly categorized into one of the following syntactic forms:
• Expression names (§6.5.6)
• Primary expressions (§15.8 - §15.13)
• Unary operator expressions (§15.14 - §15.16)
• Binary operator expressions (§15.17 - §15.24, and §15.26)
• Ternary operator expressions (§15.25)
• Lambda expressions (§15.27)
• switch expressions (§15.28)
So, the category of unary operator expressions includes cast expressions too. In that spirit, the category of binary operator expressions includes the instanceof expressions, and hence in the context of chapter 15, we can speak of instanceof as a (binary) operator.
Hope this helps,
Gavin
On 31 May 2023, at 11:14, Anton.Dil <anton.dil at open.ac.uk> wrote:
Greetings,
I have a query about which tokens should be considered operators.
Many sources say that instanceof is an operator, including the JLS, e.g. at https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.20.2
However, instanceof is not included in the list of operators here https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.12
Which is correct? Should instanceof be in the operators list?
Regards,
Anton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jls-jvms-spec-comments/attachments/20241125/26ffec78/attachment-0001.htm>
More information about the jls-jvms-spec-comments
mailing list