×

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

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.