-1

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?)

Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
  • Why is `import` a keyword? – Oliver Charlesworth Apr 02 '16 at 17:31
  • 2
    The language designers didn't really care that it _could_ be unambiguous. – Louis Wasserman Apr 02 '16 at 17:32
  • 2
    Why have keywords at all? This is a sentence "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" so why not `instanceof (instanceof instanceof instanceof)`? – Peter Lawrey Apr 02 '16 at 17:33
  • 1
    @Peter That's a perfect example why we _do_ need keywords, because the buffalo sentence is highly ambiguous. Examples in the Java grammar are "class", "int", and "this", which must be forbidden in type and variable names, or else "int.class" or "Parent.this" would be syntactically ambiguous. This question is asking whether there are technical reasons why one specific keyword, instanceof, has any similar requirement mandating its selection as a keyword. – Nicholas Wilson Apr 02 '16 at 20:59
  • @dreamcrash That question is different, it's asking why "instanceof" isn't a method, I'm asking whether there are reasons syntactically why it couldn't be an `Identifier`. – Nicholas Wilson Apr 02 '16 at 21:01
  • yep, u are right, I will delete the comment – dreamcrash Apr 02 '16 at 21:24
  • @NicholasWilson I have updated my answer based on your question about `instanceof` specifically being a keyword. – Peter Lawrey Apr 03 '16 at 07:24

1 Answers1

2

That question is different, it's asking why "instanceof" isn't a method, I'm asking whether there are reasons syntactically why

You have a point in that it could have been a method on Object or we have

if (myClass.class.isInstance(obj))

This is more cumbersome, however I would say that chains of instanceof are not considered best practice and making it a little harder might not have been a bad idea.

It is worth noting that earlier version of Java didn't use intrinsics as much as they do now and using a method would have been far less efficient than a native keyword, though I don't believe that would have to be true today.

Is instanceof a keyword simply out of tradition, rather than necessity?

IMHO keywords were/are considered good practice to make words with special meaning stand out as having and only having a special purpose.

Could new relational operators be introduced in future Java versions, with tokens that collide with identifiers?

Yes, One of the proposals for adding val and var is that they be special types, rather than keywords to avoid conflicting with code which have used them for variable names.

Given a chose, a new language would make these keywords and it is only for backward compatibility that they might be other wise. Alternatively it has been considered to use final rather than val and transient rather than var.

Personally, I think they should add them how other languages do it for consistency otherwise you are going to have every new Java developer asking basic questions like How do I compare strings in Java? What they did made sense but it confuses just about every new developer.

By comparison, they banned making _ a lambda variable to avoid confusion with other languages where this has a special meaning and they have a warning about using _ as a variable that it might be removed in future versions.

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130