If Java allowed "instanceof" as a name for variables (and fields, type names, package names), it appears, at a first glance, that the language would still remain unambiguous.
In most or all of the productions in Java where an Identifier
can appear, there are contextual cues that would prevent confusion with a binary operator.
Regarding the basic production:
RelationalExpression:
...
RelationalExpression instanceof ReferenceType
There are no expressions of the form RelationalExpression Identifier ReferenceType
, since appending a single Identifier
to any Expression
is never valid, and no ReferenceType
can be extended by adding an Identifier
on the front.
The only other reason I can think of why instanceof must be a keyword would be if there were some other production containing an Identifier
which can be broken up into an instanceof expression. That is, there may be productions which are ambiguous if we allow instanceof as an Identifier
. However, I can't seem to find any, since an Identifier
is almost always separated from its surrounding tokens by a dot (or is identifiable as a MethodName
by a following lparen).
Is instanceof a keyword simply out of tradition, rather than necessity? Could new relational operators be introduced in future Java versions, with tokens that collide with identifiers? (For example, could a hypothetical "relatedto" operator be introduced without making it a keyword, which would break existing code?)