×

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.

abstractcontinuefornewswitch
assertdefaultifpackagesynchronized
booleandogotoprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry 
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile
_ (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.


Related Topics

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.