<!-- This short description is INTENTIONALLY "none" - please see WP:SDNONE before you consider changing it! -->

thumb|A snippet of Java code with keywords highlighted in blue and bold font

In the Java programming language, a keyword is any one of 68 reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or as any other identifier. Of these 68 keywords, 17 of them are only contextually reserved, and can sometimes be used as an identifier, unlike standard reserved words. Due to their special functions in the language, most integrated development environments for Java use syntax highlighting to display keywords in a different colour for easy identification.

Reserved keywords

The following words are reserved keywords and cannot be used as identifiers under any circumstances.

;<code>abstract</code>

: A method with no definition must be declared as abstract and the class containing it must be declared as abstract. Abstract classes cannot be instantiated. Abstract methods must be implemented in the sub classes. The abstract keyword cannot be used with variables or constructors. Note that an abstract class isn't required to have an abstract method at all.

;<code>assert</code> (added in J2SE 1.4)

:Assert describes a predicate (a true–false statement) placed in a Java program to indicate that the developer thinks that the predicate is always true at that place. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort. Assertions are disabled at runtime by default, but can be enabled through a command-line option or programmatically through a method on the class loader.

:

;<code>boolean</code>

:Defines a boolean variable for the values "true" or "false" only. By default, the value of boolean primitive type is false. This keyword is also used to declare that a method returns a value of the primitive type <code>boolean</code>. In most other languages, the Boolean type is usually simply called <code>bool</code>.

;<code>break</code>

:Used to end the execution in the current loop body.

:Used to break out of a <code>switch</code> block.

;<code>byte</code>

:The <code>byte</code> keyword is used to declare a field that can hold an 8-bit signed two's complement integer. Alternatively, the <code>default</code> keyword can also be used to declare default values in a Java annotation. From Java 8 onwards, the <code>default</code> keyword can be used to allow an interface to provide an implementation of a method.

;<code>do</code>

:The <code>do</code> keyword is used in conjunction with <code>while</code> to create a do-while loop, which executes a block of statements associated with the loop and then tests a boolean expression associated with the <code>while</code>. If the expression evaluates to <code>true</code>, the block is executed again; this continues until the expression evaluates to <code>false</code>.

;<code>double</code>

:The <code>double</code> keyword is used to declare a variable that can hold a 64-bit double precision IEEE 754 floating-point number. This keyword is also used to declare that a method returns a value of the primitive type <code>double</code>.

;<code>else</code>

:The <code>else</code> keyword is used in conjunction with <code>if</code> to create an if-else statement, which tests a boolean expression; if the expression evaluates to <code>true</code>, the block of statements associated with the <code>if</code> are evaluated; if it evaluates to <code>false</code>, the block of statements associated with the <code>else</code> are evaluated.

;<code>enum</code> (added in J2SE 5.0)

:As of J2SE 5.0, the <code>for</code> keyword can also be used to create a so-called "enhanced for loop", which specifies an array or object; each iteration of the loop executes the associated block of statements using a different element in the array or <code>Iterable</code>.

;<code>instanceof</code>

:A binary operator that takes an object reference as its first operand and a class or interface as its second operand and produces a boolean result. The <code>instanceof</code> operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface.

;<code>int</code>

:The <code>int</code> keyword is used to declare a variable that can hold a 32-bit signed two's complement integer.

;<code>protected</code>

:The <code>protected</code> keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package.

;<code>super</code>

:Inheritance basically used to achieve dynamic binding or run-time polymorphism in Java. Used to access members of a class inherited by the class in which it appears. Allows a subclass to access overridden methods and hidden members of its superclass. The <code>super</code> keyword is also used to forward a call from a constructor to a constructor in the superclass.

:Also used to specify a lower bound on a type parameter in Generics.

;<code>switch</code>

:The <code>switch</code> keyword is used in conjunction with <code>case</code> and <code>default</code> to create a switch statement, which evaluates a variable, matches its value to a specific <code>case</code> (including patterns), and executes the block of statements associated with that <code>case</code>. If no <code>case</code> matches the value, the optional block labelled by <code>default</code> is executed, if included.

;<code>try</code>

:Defines a block of statements that have exception handling. If an exception is thrown inside the <code>try</code> block, an optional <code>catch</code> block can handle declared exception types. Also, an optional <code>finally</code> block can be declared that will be executed when execution exits the <code>try</code> block and <code>catch</code> clauses, regardless of whether an exception is thrown or not. A <code>try</code> block must have at least one <code>catch</code> clause or a <code>finally</code> block.

;<code>void</code>

:The <code>void</code> keyword is used to declare that a method does not return any value. Methods, classes and interfaces thus cannot be declared volatile, nor can local variables or parameters.

;<code>while</code>

:The <code>while</code> keyword is used to create a while loop, which tests a boolean expression and executes the block of statements associated with the loop if the expression evaluates to <code>true</code>; this continues until the expression evaluates to <code>false</code>. This keyword can also be used to create a do-while loop; see <code>do</code>.

;<code>const</code>

:Although reserved as a keyword in Java, <code>const</code> is not used and has no function. Previously this keyword was used to restrict the precision and rounding of floating point calculations to ensure portability.

Contextual keywords

The following identifiers are contextual keywords, and are only restricted in some contexts:

;<code>exports</code>

:Used in a module declaration to specify which packages are available to other modules.

;<code>module</code>

:Declares a module (a collection of related packages and resources that can be treated as a unit), used to encapsulate and expose only the public API of a library.

;<code>non-sealed</code>

:Used to declare that a class or interface which extends a sealed class can be extended by unknown classes.

;<code>open</code>

:Indicates that all classes in a package are accessible via reflection by other modules.

;<code>opens</code>

:Used to open a specific package for reflection to other modules.

;<code>permits</code>

:The permits clause specifies the classes that are permitted to extend a sealed class. Used to declare a variable without explicitly specifying the type, rather relying on the compiler to infer the type based on the initialiser.

;<code>when</code>

:Used as an additional check for a <code>case</code> statement.

;<code>with</code>

:Used with the <code>provides</code> directive to specify which implementation of a service is provided by the module.

;<code>yield</code>

:Used to set a value for a switch expression, when using labelled statement groups (for example, <code>case L:</code>).

Reserved words for literal values

The following words refer to literal values used by the language.

;<code>true</code>

:A boolean literal value.

;<code>false</code>

:A boolean literal value.

;<code>null</code>

:A reference literal value.

See also

  • Java syntax
  • Java annotation

References