Contextual keywords in Java
Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar.
These are the keywords in the code that have a specific significance. The words "abstract," "new," "final," "try," "import," etc. are not among the reserved keywords.
ASCII character sequences can only be used as keywords; they cannot be used as identifiers. Depending on the context in which they appear, additional 12-character sequences similarly made up of ASCII characters may be read as keywords.
In this sense, reserved keywords and contextual keywords are two distinct concepts.
Reserved keywords
Keywords in a language have a specific meaning and are part of the syntax. Because the language reserves them, reserved words cannot be used as identifiers (variables, functions, and so on). The majority of keywords are reserved words in practice and vice versa. In Java, keywords are reserved words that serve as code keys. Because they are predefined, these words cannot be used for anything else.
The reserved keywords in Java are mentioned below.
abstract | continue | for | new | switch |
assert | default | if | package | synchronized |
boolean | do | goto | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extendsint | short | try | |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const | float | native | super | while |
_ (underscore) |
Contextual keywords
The Java contextual keywords are listed below:
- exports opens to var
- the module provides transitive with
- open requires uses yield
Suppose any character in a character sequence that matches a contextual keyword may be concatenated with characters immediately before or after it to create a distinct token. In that case, the character sequence is not considered a keyword.
Contextual keywords are identifiers that only function as keywords in specific language structures. The distinction between context and keywords is that context keywords can be used as identifiers in other parts of the code. Exact context keywords (partial and where) have different meanings in different contexts.
Therefore, even at the beginning of a ModuleDeclaration, the character sequence openmodule is understood as a single identifier rather than two contextual keywords. Two keywords must be separated by whitespace or comment if they are intended to be used together.
Regarding how an implementation might disambiguate, we are purposefully being ambiguous. It should be evident from the remark above that we are not going to declare this explicitly; instead, this is an implementation choice. However, as the language changes, designers need extra caution when implementing new contextual keywords in specific scenarios.
Many character sequences are mistakenly interpreted as keywords from time to time. For example, true and false are boolean literals rather than keywords. The actual null is used instead of the term null as a keyword. A name's meaning changes depending on the situation it is used.
Three steps are necessary to determine a name's meaning:
- In the first place, context determines which of the seven syntactic categories a name belongs to: ModuleName, PackageName, TypeName, ExpressionName, MethodName, PackageOrTypeName, or AmbiguousName.
Due to their use of TypeIdentifier and UnqualifiedMethodIdentifier, respectively, TypeName and MethodName are less expressive than the other five categories.
- Second, a name previously categorized as an AmbiguousName, a PackageOrTypeName, or an ExpressionName by its context is subsequently reclassified as a PackageName, TypeName, or ExpressionName.
- Third, the final interpretation of the name's meaning is dictated by the resulting category (or a compile-time error if the name has no meaning).
The usage of context reduces name conflicts between entities of various types. If the name guidelines outlined above are followed, conflicts of this nature will be infrequent. But as types created by other programmers or organizations proliferate, conflicts could unintentionally occur. For instance, a name may be shared by types, methods, and fields. Because the context of use always indicates if a method is intended, it is always feasible to tell apart a method from a field with the same name.
Only in specific language forms do some identifiers serve as keywords. When compared to keywords, contextual keywords can serve as identifiers in other areas of the code. For example, the words “partial” and “where” have different meanings in different contexts. Therefore, there are many such words that have different meanings in different contexts.