
In theJava programming language, akeyword is any one of 68reserved words[1] that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names forvariables,methods,classes, or as any otheridentifier.[2] 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, mostintegrated development environments for Java usesyntax highlighting to display keywords in a different colour for easy identification.
The following words are reserved keywords and cannot be used as identifiers under any circumstances.
_abstractassert (added inJ2SE 1.4)[4]booleanboolean. In most other languages, the Boolean type is usually simply calledbool.bytebyte keyword is used to declare a field that can hold an 8-bit signedtwo's complement integer.[5][6] This keyword is also used to declare that a method returns a value of the primitive typebyte.[7][8]caseswitch block can be labeled with one or morecase ordefault labels. Theswitch statement evaluates its expression, then executes all statements that follow the matchingcase label; seeswitch.[9][10]catchtry block and an optionalfinally block. The statements in thecatch block specify what to do if a specific type of exception is thrown by thetry block.charclassObject. The class keyword can also be used in the formClass.class to get aClass object without needing an instance of that class. For example,String.class can be used instead of doingnewString().getClass().continuecontinue resumes execution at the end of the enclosing labeled loop body.defaultdefault keyword can optionally be used in aswitch statement to label a block of statements to be executed if nocase matches the specified value; seeswitch.[9][10] Alternatively, thedefault keyword can also be used to declare default values in aJava annotation. From Java 8 onwards, thedefault keyword can be used to allow an interface to provide an implementation of a method.dodo keyword is used in conjunction withwhile to create ado-while loop, which executes a block of statements associated with the loop and then tests a boolean expression associated with thewhile. If the expression evaluates totrue, the block is executed again; this continues until the expression evaluates tofalse.[11][12]doubledouble keyword is used to declare a variable that can hold a 64-bitdouble precisionIEEE 754floating-point number.[5][6] This keyword is also used to declare that a method returns a value of the primitive typedouble.[7][8]elseelse keyword is used in conjunction withif to create anif-else statement, which tests aboolean expression; if the expression evaluates totrue, the block of statements associated with theif are evaluated; if it evaluates tofalse, the block of statements associated with theelse are evaluated.[13][14]enum (added inJ2SE 5.0)[4]Enum.extendsfinalfinal.finallytry keyword. Thefinally block is executed after execution exits thetry block and any associatedcatch clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of thetry orcatch blocks using thereturn keyword.floatfloat keyword is used to declare a variable that can hold a 32-bitsingle precision IEEE 754 floating-point number.[5][6] This keyword is also used to declare that a method returns a value of the primitive typefloat.[7][8]forfor keyword is used to create afor loop, which specifies a variable initialization, aboolean expression, and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates totrue, the block of statements associated with the loop are executed, and then the incrementation is performed. The boolean expression is then evaluated again; this continues until the expression evaluates tofalse.[15]for keyword can also be used to create a so-called "enhanced for loop",[16] which specifies anarray orIterable object; each iteration of the loop executes the associated block of statements using a different element in the array orIterable.[15]ifif keyword is used to create anif statement, which tests aboolean expression; if the expression evaluates totrue, the block of statements associated with the if statement is executed. This keyword can also be used to create anif-else statement; seeelse.[13][14]implementsimportimport statements can importstatic members of a class. AJava module may itself be imported (by writingimport module), automatically importing all exported packages.[17]instanceofinstanceof operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface.intint keyword is used to declare a variable that can hold a 32-bit signed two's complement integer.[5][6] This keyword is also used to declare that a method returns a value of the primitive typeint.[7][8]interfacestatic final) fields andstatic interfaces. It can later be implemented by classes that declare the interface with theimplements keyword. Asmultiple inheritance is not allowed in Java, interfaces are used to circumvent it. An interface can be defined within another interface.longlong keyword is used to declare a variable that can hold a 64-bit signed two's complement integer.[5][6] This keyword is also used to declare that a method returns a value of the primitive typelong.[7][8]nativenewpackagepackage keyword.privateprivate keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class.[18]protectedprotected 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'ssubclasses or classes from the samepackage.[18]publicpublic keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class.[18]returnshortshort keyword is used to declare a field that can hold a 16-bit signed two's complement integer.[5][6] This keyword is also used to declare that a method returns a value of the primitive typeshort.[7][8]staticstatic also is used to define a method as a class method. Class methods arebound to the class instead of to a specific instance, and can only operate on class fields. Classes and interfaces declared asstatic members of another class or interface are behaviorally top-level classes.[19]supersuper keyword is also used to forward a call from a constructor to a constructor in the superclass.switchswitch keyword is used in conjunction withcase anddefault to create aswitch statement, which evaluates a variable, matches its value to a specificcase (includingpatterns), and executes the block of statements associated with thatcase. If nocase matches the value, the optional block labelled bydefault is executed, if included.[9][10] The switch keyword can also be used with the non-reserved keywordyield to create switch expressions.synchronizedClass. Guarantees that at most one thread at a time operating on the same object executes that code. The mutex lock is automatically released when execution exits the synchronized code. Fields, classes and interfaces cannot be declared assynchronized.thisthis can be used to access class members and as a reference to the current instance. Thethis keyword is also used to forward a call from one constructor in a class to another constructor in the same class.throwcatch keyword to handle an assignment compatible exception type. If no such exception handler is found in the current method, then the method returns and the process is repeated in the calling method. If no exception handler is found in any method call on the stack, then the exception is passed to the thread's uncaught exception handler.throwsException but notRuntimeException) must be declared using thethrows keyword.transienttransient keywords are ignored.[20][21]trytry block, an optionalcatch block can handle declared exception types. Also, an optionalfinally block can be declared that will be executed when execution exits thetry block andcatch clauses, regardless of whether an exception is thrown or not. Atry block must have at least onecatch clause or afinally block.volatilewhilewhile keyword is used to create awhile loop, which tests aboolean expression and executes the block of statements associated with the loop if the expression evaluates totrue; this continues until the expression evaluates tofalse. This keyword can also be used to create ado-while loop; seedo.[11][12]The following words are reserved as keywords, but currently have no use or purpose.
constconst is not used and has no function.[2][23] In other languages,const is typically used to define constants. For defining constants in Java, see thefinal keyword.gotogoto is not used and has no function.[2][23] In other languages,goto is typically used as a one-way control statement to jump to a label at another line of code.strictfp (added inJ2SE 1.2)[4]strictfp is obsolete, and no longer has any function.[24] Previously this keyword was used to restrict the precision and rounding of floating point calculations to ensure portability.[8]The following identifiers are contextual keywords, and are only restricted in some contexts:
exportsmodulenon-sealedopenopenspermitsprovidesrecord.equals(),.hashCode(), and.toString() methods.requiressealedtoopens directive to specify which module is allowed to reflectively access the package.transitiverequires directive to indicate that a module not only requires another module but also makes that module's dependencies available to modules that depend on it.usesvarwhencase statement.[27]withprovides directive to specify which implementation of a service is provided by the module.yieldcase L:).[28]The following words refer to literal values used by the language.
truefalsenull