List of Java keywords: Difference between revisions

Content deleted Content added
m grammar (pluralization)
Link suggestions feature: 2 links added.
 
(382 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|none}} <!-- This short description is INTENTIONALLY "none" - please see WP:SDNONE before you consider changing it! -->
[[Image:Java keywords highlighted.png|thumb|A snippet of Java code with keywords highlighted in blue and bold font]]
[[File:Java keywords highlighted.svg|thumb|A snippet of Java code with keywords highlighted in blue and bold font]]
 
In the [[Java (programming language)|Java programming language]], a '''keyword''' is any one of 5068 [[reserved word]]s<ref>{{Cite whichweb|title=Java Platform, Standard Edition Java API Reference|url=https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-3.9}}</ref> that have a predefined meaning in the language;. becauseBecause of this, programmers cannot use keywords in some contexts, such as names for [[variable#Computer (programming)|variables]], [[method (computer science)|methods]], [[class (computer science)|classes]], or as any other [[identifier]].<ref name="keywords">{{cite web |title=Java Language Specification - Section 3.9: Keywords |url=httphttps://javadocs.sunoracle.com/docsjavase/booksspecs/tutorialjls/javase11/nutsandboltshtml/_keywordsjls-3.html#jls-3.9 |work=The Java TutorialsLanguage Specification |publisher=[[SunOracle Microsystems, Inc.Corporation|Oracle]] |yeardate=20082018-08-21 |monthaccess-date=February |accessdate=20082018-12-0225}}</ref> 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 environment]]s for Java use [[syntax highlighting]] to display keywords in a different colorcolour for easy identification.{{fact}}
 
==Keywords==
The following is a list of the keywords in Java, along with a brief description of their functions:<ref name="keywords" />
The following words are keywords and cannot be used as identifiers under any circumstances.
;<code>_</code>
:Added in Java 9, the underscore has become a keyword and cannot be used as a variable name anymore.<ref>{{cite web |title=Treatment of underscores|website=JEP 302: Lambda Leftovers |url=https://openjdk.org/jeps/302#Treatment-of-underscores}}</ref>
 
;<code>[[Abstractabstract classtype|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.
:Used in a class declaration to specify that a class is not to be instantiated, but rather extended by other classes. Used in a method declaration to declare a method without providing the implementation. An abstract class can have abstract methods that are not implemented in the abstract class, but in [[Subclass (computer science)|subclasses]]. All methods declared in an <code>interface</code> are implicitly <code>abstract</code>.
 
;<code>[[assertion (software development)|assert]]</code> (added in [[J2SE 1.4]])<ref name="keywords date">{{cite web |title=Java Language Keywords |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |accessdate=2017-07-24}}</ref>
;[[Assertion (computing)|assert]] (as of 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.
:A keyword used to make the assumed value of a condition explicit. If the condition is not true, an {{Javadoc:SE|java/lang|AssertionError}} is thrown.
:
;<code>[[boolean data type|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 data type|boolean]]</code>. In most other languages, the Boolean type is usually simply called <code>bool</code>.
 
;<code>[[break statement|break]]</code>
;[[Boolean datatype#Java|boolean]]
:Used to end the execution in the current loop body.
:Refers to an expression or [[variable#Computer Programming|variable]] that can have only a true or false value. Java provides the <code>boolean</code> type and the literal values <code>true</code> and <code>false</code>.
:Used to break out of a <code>[[#switch|switch]]</code> block.
 
;<code>[[byte]]</code>
;[[Switch statement#Java|break]]
:The <code>byte</code> keyword is used to declare a field that can hold an 8-bit signed [[two's complement]] integer.<ref name="primitive"/>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>byte</code>.<ref name="return"/>{{sfn|Flanagan|2005|pp=66-67}}
:Used to resume program execution at the statement immediately following the current enclosing block or statement. If followed by a [[Label (programming language)|label]], the program resumes execution at the statement immediately following the enclosing labeled statement or block.
 
;<code>[[switch statement|case]]</code>{{Anchor|case}}
;[[byte]]
:A statement in the <code>[[#switch|switch]]</code> block can be labeled with one or more <code>[[#case|case]]</code> or <code>[[#default|default]]</code> labels. The <code>[[#switch|switch]]</code> statement evaluates its expression, then executes all statements that follow the matching <code>[[#case|case]]</code> label; see <code>[[#switch|switch]]</code>.<ref name="switch"/>{{sfn|Flanagan|2005|pp=46-48}}
:An 8-[[bit]] integer. A keyword used to [[Declaration (computer science)|declare]] an expression, method return value, or variable of type byte.
 
;<code>[[Switchexception statementhandling syntax#Java|casecatch]]</code>
:Used in conjunction with a <code>try</code> block and an optional <code>finally</code> block. The statements in the <code>catch</code> block specify what to do if a specific type of exception is thrown by the <code>try</code> block.
:Defines a group of statements to execute if the specified value matches the value defined by the enclosing <code>switch</code> statement.
 
;<code>[[character (computing)|char]]</code>
;[[Exception handling syntax#Java|catch]]
:Defines a character variable capable of holding any character of the Java source file's character set.
:Defines an [[exception handler]]&mdash;a group of statements that are executed if an exception is thrown in the block defined by a preceding <code>try</code> keyword. The code is executed only if the class of the thrown exception is assignment compatible with the exception class declared by the <code>catch</code> clause.
 
;<code>[[class (computer science)#Java|class]]</code>
;[[Character (computing)|char]]
:A type that defines the implementation of a particular kind of object. A class definition defines [[Object (computer science)|instance]] and class [[field (computer science)|fields]], [[method (computer science)|methods]], and [[inner class]]es as well as specifying the [[interface (computer science)|interfaces]] the class implements and the immediate [[superclass (computer science)|superclass]] of the class. If the superclass is not explicitly specified, the superclass is implicitly {{Javadoc:SE|java/lang|Object}}. The class keyword can also be used in the form <code>Class.class</code> to get a <code>Class</code> object without needing an instance of that class. For example, <syntaxhighlight lang="Java" inline>String.class</syntaxhighlight> can be used instead of doing <syntaxhighlight lang="Java" inline>new String().getClass()</syntaxhighlight>.
:A 16-bit [[Unicode]] character. A Java keyword used to declare an expression, method return value, or variable of type character.
 
;<code>[[continue (Java)|continue]]</code>
;[[Class (computer science)#Java|class]]
:Used to resume program execution at the end of the current loop body. If followed by a label, <code>continue</code> resumes execution at the end of the enclosing labeled loop body.
:A type that defines the implementation of a particular kind of object. A class definition defines [[Object (computer science)|instance]] and class fields, [[Method (computer science)|methods]], and [[inner class]]es as well as specifying the [[Interface (computer science)|interfaces]] the class implements and the immediate [[Superclass (computer science)|superclass]] of the class. If the superclass is not explicitly specified, the superclass is implicitly {{Javadoc:SE|java/lang|Object}}.
 
;<code>default</code>{{Anchor|default}}
;[[Variable#Constant|const]] (reserved without use)
:The <code>default</code> keyword can optionally be used in a [[switch statement]] to label a block of statements to be executed if no <code>case</code> matches the specified value; see ''<code>[[#switch|switch]]</code>''.<ref name="switch">{{cite web |title=The switch Statement |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2014-12-18}}</ref>{{sfn|Flanagan|2005|pp=46-48}} 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.
:This keyword is '''not used''' by current versions of the Java programming language.
 
;<code>[[do while loop|do]]</code>{{Anchor|do}}
;[[continue (Java)|continue]]
:The <code>do</code> keyword is used in conjunction with <code>[[#while|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>.<ref name="do-while">{{cite web |title=The while and do-while Statements |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>{{sfn|Flanagan|2005|pp=48-49}}
:Used to resume program execution at the end of the current loop body. If followed by a label, <code>continue</code> resumes execution at the end of the enclosing labeled loop body.
 
;<code>[[double precision|double]]</code>
;[[Switch statement#Java|default]]
:The <code>double</code> keyword is used to declare a variable that can hold a 64-bit [[double precision]] [[IEEE 754]] [[floating-point number]].<ref name="primitive">{{cite web |title=Primitive Data Types |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>double</code>.<ref name="return">{{cite web |title=Returning a Value from a Method |url=https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>{{sfn|Flanagan|2005|pp=66-67}}
:Defines a group of statements to begin executing if the value defined by the enclosing <code>switch</code> statement does not match any value specified by a <code>case</code> keyword in the <code>switch</code> statement.
 
;<code>[[conditional (programming)|else]]</code>{{Anchor|else}}
;[[do while loop#Java|do]]
:The <code>else</code> keyword is used in conjunction with <code>[[#if|if]]</code> to create an [[conditional (programming)|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.<ref name="if-else">{{cite web |title=The if-then and if-then-else Statements |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>{{sfn|Flanagan|2005|pp=44-46}}
:Used to declare a loop that will [[Iteration|iterate]] a block of statements. The loop's exit condition is specified with the <code>while</code> keyword. The loop will execute once before evaluating the exit condition.
 
;<code>[[enumerated type|enum]]</code> (added in [[J2SE 5.0]])<ref name="keywords date"/>
;[[Double precision|double]]
:A 64-bit [[Floating Point|floating point value]]. A Java keyword used to declare an expression, method return value, or variable of type double-precision floating point number.
 
;[[Conditional statement|else]]
:Used to define a statement or block of statements that are executed in the case that the test condition specified by the <code>if</code> keyword evaluates to false.
 
;[[Enumerated type|enum]] (as of J2SE 5.0)
:A Java keyword used to declare an [[enumerated type]]. Enumerations extend the base class {{Javadoc:SE|java/lang|Enum}}.
 
;<code>[[Inheritanceinheritance (object-oriented programming)#Java|extends]]</code>
:Used in a class declaration to specify the superclass; used in an interface declaration to specify one or more superinterfaces. Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. An interface Z extends one or more interfaces by adding methods. Class X is said to be a subclass of class Y; Interface Z is said to be a subinterface of the interfaces it extends.
:Also used to specify an upper bound on a type parameter in Generics.
 
;<code>[[final (Java)|final]]</code>
:Define an entity once that cannot be changed nor derived from later. More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression on an executed command. All methods in a final class are implicitly <code>final</code>.
 
;<code>[[Exceptionexception handling syntax#Java|finally]]</code>
:Used to define a block of statements for a block defined previously by the <code>try</code> keyword. The <code>finally</code> block is executed after execution exits the <code>try</code> block and any associated <code>catch</code> clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the <code>try</code> or <code>catch</code> blocks using the <code>return</code> keyword.
 
;<code>[[Floatingsingle pointprecision|float]]</code>
:The <code>float</code> keyword is used to declare a variable that can hold a 32-bit [[single precision]] IEEE 754 floating-point number.<ref name="primitive"/>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>float</code>.<ref name="return"/>{{sfn|Flanagan|2005|pp=66-67}}
:A 32-bit floating point value. A Java keyword used to declare an expression, method return value, or variable of type single-precision floating point number.
 
;<code>[[for loop#Java|for]]</code>
:The <code>for</code> keyword is used to create a [[for loop]], which specifies a variable initialization, a [[boolean expression]], and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates to <code>true</code>, 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 to <code>false</code>.<ref name="for">{{cite web |title=The for Statement |url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
:Used to define a loop that reiterates statements. The <code>for</code> loop specifies the statements to be executed, exit condition, and initialization variables for the loop. The exit condition is evaluated before the first iteration of the loop. Since J2SE 5.0, a form of the <code>for</code> loop specifies an {{Javadoc:SE|java/lang|Iterable}} object where each iteration of the loop processes one of its contained elements.
 
:As of [[J2SE 5.0]], the <code>for</code> keyword can also be used to create a so-called "[[foreach|enhanced for loop]]",{{sfn|Flanagan|2005|pp=50-54}} which specifies an [[array data type|array]] or {{Javadoc:SE|java/lang|Iterable}} object; each iteration of the loop executes the associated block of statements using a different element in the array or <code>Iterable</code>.<ref name="for"/>
;[[GOTO|goto]] (reserved without use)
:This keyword is '''not used''' by current versions of the Java programming language.
 
;<code>[[Conditionalif statement|if]]</code>{{Anchor|if}}
:The <code>if</code> keyword is used to create an [[if statement]], which tests a [[boolean expression]]; if the expression evaluates to <code>true</code>, the block of statements associated with the if statement is executed. This keyword can also be used to create an [[conditional (programming)|if-else statement]]; see ''<code>[[#else|else]]</code>''.<ref name="if-else"/>{{sfn|Flanagan|2005|pp=44-46}}
:Used to conduct a conditional test and execute a block of statements if the test evaluates to true. If an optional block is defined with the <code>else</code> keyword, then the <code>else</code> block is executed if the test evaluates to false.
 
;<code>implements</code>
:Included in a class declaration to specify one or more [[Interfaceinterface (Java)|interfaces]] that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces.
 
;<code>import</code>{{Anchor|import}}
;import
:Used at the beginning of a [[source file]] to specify classes or entire [[Java package]]s to be referred to later without including their package names in the reference. Since J2SE 5.0, <code>import</code> statements can import <code>static</code> members of a class. A [[Java Platform Module System|Java module]] may itself be imported (by writing <code>import module</code>), automatically importing all exported packages.<ref>{{Cite web|url=https://openjdk.org/jeps/494|title=JEP 494: Module Import Declarations (Second Preview)|website=openjdk.org}}</ref>
 
;<code>instanceof</code>
:A [[operator (programming)|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>[[Integerinteger (computer science)|int]]</code>
:The <code>int</code> keyword is used to declare a variable that can hold a 32-bit signed two's complement integer.<ref name="primitive"/>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>int</code>.<ref name="return"/>{{sfn|Flanagan|2005|pp=66-67}}
:A 32-bit integer value. A Java keyword used to declare an expression, method return value, or variable of type integer.
 
;<code>[[Interfaceinterface (Java)|interface]]</code>
:Used to declare aan special[[Interface type of class(Java)|interface]] that only contains abstract or default methods, constant (<code>static final</code>) fields and <code>static</code> interfaces. It can later be implemented by classes that declare the interface with the <code>implements</code> keyword. As [[multiple inheritance]] is not allowed in Java, interfaces are used to circumvent it. An interface can be defined within another interface.
 
;<code>[[long integer|long]]</code>
:The <code>long</code> keyword is used to declare a variable that can hold a 64-bit signed two's complement integer.<ref name="primitive"/>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>long</code>.<ref name="return"/>{{sfn|Flanagan|2005|pp=66-67}}
:A 64-bit integer value. A Java keyword used to declare an expression, method return value, or variable of type long integer.
 
;<code>[[Java Native Interface|native]]</code>
;native
:Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language. {{sfn|Flanagan|2005|pp=66-67}}
 
;<code>[[Objectobject lifetime#Java|new]]</code>{{Anchor|new}}
:Used to create an instance of a class or array object. Using keyword for this end is not completely necessary (as exemplified by [[Scala (programming language)|Scala]]), though it serves two purposes: it enables the existence of different namespace for methods and class names, it defines statically and locally that a fresh object is indeed created, and of what runtime type it is (arguably introducing dependency into the code).
:Used to create an instance of a class or array.
 
;<code>[[Java package|package]]</code>
:AJava package is a group of typessimilar classes and interfaces. Packages are declared with the <code>package</code> keyword.
 
;<code>[[b:Java Programming/Classes, Objects and Types|private]]</code>
;[[Java_syntax#Class_member_access|private]]
:The <code>private</code> 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.<ref name="access">{{cite web |title=Controlling Access to Members of a Class |url=https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
:An access modifier used in a method, field or inner class declaration. It signifies that the member can only be accessed by other elements of its class.
 
;<code>[[Java_syntax#Class_member_accessb:Java Programming/Classes, Objects and Types|protected]]</code>
:AnThe access<code>protected</code> modifierkeyword is used in the declaration of a method, field, or inner class; declaration.protected It signifies that the membermembers can only be accessed by elementsmembers residingof intheir itsown class, that class's [[inheritance (object-oriented programming)|subclasses,]] or classes infrom the same [[Java package|package]].<ref name="access"/>
 
;<code>[[b:Java Programming/Classes, Objects and Types|public]]</code>
;[[Java_syntax#Class_member_access|public]]
:AnThe access<code>public</code> modifierkeyword is used in the declaration of a class, method, or field; declaration.public Itclasses, signifies that the classmethods, method orand variablefields can be accessed by elementsthe residingmembers inof otherany classes or packagesclass. All members declared in an <code>interface</code>ref are implicitly <code>public<name="access"/code>.
 
;<code>[[Methodmethod (computer science)|return]]</code>
:Used to finish the execution of a method. It can be followed by a value required by the method definition that is returned to the caller.
 
;<code>[[short integer|short]]</code>
:The <code>short</code> keyword is used to declare a field that can hold a 16-bit signed two's complement integer.<ref name="primitive"/>{{sfn|Flanagan|2005|p=22}} This keyword is also used to declare that a method returns a value of the primitive type <code>short</code>.<ref name="return"/>{{sfn|Flanagan|2005|pp=66-67}}
:A 16-bit integer value. Used to declare an expression, method return value, or variable of type short integer.
 
;<code>[[Staticstatic variable|static]]</code>
:Used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. <code>static</code> also is used to define a method as a class method. Class methods are [[Namename binding|bound]] to the class instead of to a specific instance, and can only operate on class fields. (Classes and interfaces declared as <code>static</code> members of another class or interface are actuallybehaviorally top-level classes.<ref>{{cite andweb are| ''not''url=https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html inner| classes.title=Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) }}</ref>
 
;<code>[[inheritance (object-oriented programming)|super]]</code>
;[[strictfp]] (as of J2SE 1.2)
: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 [[method overriding (programming)|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.
:A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability.
 
;[[Inheritance (object-oriented programming)#Java|super]]
:Used to access members of a class inherited by the class in which it appears. Allows a subclass to access [[Method overriding (programming)|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>[[Switchswitch statement#Java|switch]]</code>{{Anchor|switch}}
:The <code>switch</code> keyword is used in conjunction with <code>[[#case|case]]</code> and <code>[[#default|default]]</code> to create a [[switch statement]], which evaluates a variable, matches its value to a specific <code>case</code> (including [[pattern matching|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.<ref name="switch"/>{{sfn|Flanagan|2005|pp=46-48}} The switch keyword can also be used with the non-reserved keyword <code>yield</code> to create switch expressions.
:Used to evaluate a variable that can later be matched with a value specified by the <code>case</code> keyword in order to execute a group of statements.
 
;<code>[[Mutualmutual exclusion|synchronized]]</code>{{Anchor|synchronized}}
:Used in the declaration of a method or code block to acquire the [[mutex]] lock for an object while the current [[thread (computer science)|thread]] executes the code.{{sfn|Flanagan|2005|pp=66-67}} For static methods, the object locked is the class's <code>Class</code>. 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 as ''synchronized''.
 
;<code>[[this (Java)|this]]</code>
:Used to represent an instance of the class in which it appears. <code>this</code> can be used to access class members and as a reference to the current instance. The <code>this</code> keyword is also used to forward a call from one constructor in a class to another constructor in the same class.
 
;<code>[[Exceptionexception handling syntax#Java|throw]]</code>
:Causes the declared exception instance to be thrown. This causes execution to continue with the first enclosing exception handler declared by the <code>catch</code> 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.
 
;<code>[[Exceptionexception handling syntax#Java|throws]]</code>
:Used in method declarations to specify which exceptions are not handled within the method but rather passed to the next higher level of the program. All uncaught exceptions in a method that are not instances of <code>RuntimeException</code> must be declared using the <code>throws</code> keyword.
 
;<code>[[transient (computer programming)|transient]]</code>
:Declares that an instance field is not part of the default [[Serializationserialization|serialized]] form of an object. When an object is serialized, only the values of its non-transient instance fields are included in the default serial representation. When an object is deserialized, transient fields are initialized only to their default value. If the default form is not used, e.g. when a ''serialPersistentFields'' table is declared in the class hierarchy, all <code>transient</code> keywords are ignored.<ref name="serialSpec">{{cite web |title=Java Object Serialization Specification version 1.5.0 |at=1.5 Defining Serializable Fields for a Class |url=http://download-llnw.oracle.com/javase/1.5.0/docs/guide/serialization/spec/serial-arch.html#6250 |publisher=Sun/Oracle |year=2004 |accessdate=2010-09-16 }}{{Dead link|date=March 2020 |bot=InternetArchiveBot |fix-attempted=yes }}</ref><ref name="transient1">
{{cite web |first=William |last=Grosso |title=Java RMI: Serialization |url=http://onjava.com/pub/a/onjava/excerpt/JavaRMI_10/index.html?page=3 |at=Declaring serialPersistentFields |work=ONJava |publisher=O'Reilly Media |date=November 21, 2001 |accessdate=2010-09-16}}
</ref>
 
;<code>[[Exceptionexception handling syntax#Java|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 return type|void]]</code>
:UsedThe in<code>void</code> methodkeyword declarationsis used to specifydeclare that thea method does not return any value.<ref <code>void<name="return"/code> can also be used as a nonfunctional statement.
 
;<code>[[Volatilevolatile variable|volatile]]</code>
:Used in field declarations to specifyguarantee thatvisibility theof changes to variables across threads. Every read of a volatile variable iswill modifiedbe read from main memory, and not from the [[asynchronousCPU cache]]ly, byand concurrentlythat runningevery threadswrite to a volatile variable will be written to main memory, and not just to the CPU cache.<ref name="Java Volatile Keyword">{{cite web | title=Java Volatile Keyword | url=http://tutorials.jenkov.com/java-concurrency/volatile.html}}</ref> Methods, classes and interfaces thus cannot be declared ''volatile'', nor can local variables or parameters.
 
;<code>[[do while loop#Java|while]]</code>{{Anchor|while}}
: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|do]]</code>''.<ref name="do-while"/>{{sfn|Flanagan|2005|pp=48-49}}
:Used to declare a loop that iterates a block of statements. The loop's exit condition is specified as part of the while statement. If <code>while</code> appears before the body of the loop, the exit condition is evaluated before the first iteration. If <code>while</code> appears after the loop body then the <code>do</code> keyword designates the beginning of the loop body which is executed once before evaluating the exit condition.
 
==[[Reserved word]]s for [[literal]] valuesIdentifiers==
The following identifiers are contextual keywords, and are only restricted in some contexts:
 
;<code>exports</code>
;[[Truth value|false]]
:Used in a module declaration to specify which packages are available to other modules.
 
;<code>[[Java Platform Module System|module]]</code>
:Declares a [[Modular programming|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.<ref name="sealed">{{cite web|title=Sealed Classes|url=https://docs.oracle.com/en/java/javase/15/docs/specs/sealed-classes-jls.html#jls|access-date=2021-08-07|website=docs.oracle.com|publisher=Oracle Corporation}}</ref>
 
;<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.<ref name="sealed" />
 
;<code>provides</code>
:Used to declare that a module provides an implementation of a service interface.
 
;<code>[[Record (computer science)|record]]</code>
:A special kind of class that acts as a transparent carrier of immutable data, automatically providing <code>.equals()</code>, <code>.hashCode()</code>, and <code>.toString()</code> methods.
 
;<code>requires</code>
:Used in a module declaration to specify that the module depends on another module.
 
;<code>sealed</code>
:A sealed class or interface can only be extended or implemented by classes and interfaces permitted to do so.<ref name="sealed" />
 
;<code>to</code>
:Used with the <code>opens</code> directive to specify which module is allowed to reflectively access the package.
 
;<code>transitive</code>
:Used with the <code>requires</code> 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.
 
;<code>uses</code>
:Used in a module to declare that the module is using a service (i.e. it will consume a service provided by other modules).
 
;<code>[[Type inference|var]]</code>
:A special identifier that cannot be used as a type name (since Java 10).<ref>{{cite web |url=https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9|title=Chapter 3. Lexical Structure|website=docs.oracle.com|access-date=2018-12-25}}</ref> 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. <ref> {{cite web |url=https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.1|title=Chapter 14. Blocks, Statements, and Patterns}}</ref>
 
;<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>).<ref>{{cite web |title=Switch Expressions |url=https://docs.oracle.com/en/java/javase/16/language/switch-expressions.html |publisher=Oracle Corporation |website=docs.oracle.com |access-date=2020-12-27}}</ref>
 
==Reserved words for literal values==
The following words refer to literal values used by the language.
 
;<code>[[truth value|true]]</code>
:A boolean literal value.
 
;<code>[[truth value|false]]</code>
;[[null (computer programming)|null]]
:A boolean literal value.
 
;<code>[[null pointer|null]]</code>
:A reference literal value.
 
==Unused==
;[[Truth value|true]]
The following words are reserved as keywords, but currently have no use or purpose.
:A boolean literal value.
 
;<code>[[constant (programming)|const]]</code>
:Although reserved as a keyword in Java, <code>const</code> is not used and has no function.<ref name="keywords"/>{{sfn|Flanagan|2005|p=20}} In other languages, <code>const</code> is typically used to define constants. For defining constants in Java, see the <code>final</code> keyword.
 
;<code>[[GOTO|goto]]</code>
:Although reserved as a keyword in Java, <code>goto</code> is not used and has no function.<ref name="keywords"/>{{sfn|Flanagan|2005|p=20}} In other languages, <code>goto</code> is typically used as a one-way control statement to jump to a label at another line of code.
 
;<code>[[strictfp]]</code> (added in [[J2SE 1.2]])<ref name="keywords date" />
:Although reserved as a keyword in Java, <code>strictfp</code> is obsolete, and no longer has any function.<ref>{{Cite web|title=JEP 306: Restore Always-Strict Floating-Point Semantics|url=https://openjdk.java.net/jeps/306}}</ref> Previously this keyword was used to restrict the precision and rounding of floating point calculations to ensure portability.{{sfn|Flanagan|2005|pp=66-67}}
 
==See also==
*[[Java syntax]]
*[[Java annotation]]
 
==References==
{{Reflist}}
 
==External links==
{{Refbegin}}
*{{cite book |last1=Gosling |first1=James |authorlink1=James Gosling |last2=Joy |first2=Bill |authorlink2=Bill Joy |last3=Steele |first3=Guy |authorlink3=Guy Steele |last4=Bracha |first4=Gilad |authorlink4=Gilad Bracha |title=Java Language Specification |edition=Third |publisher=[[Addison-Wesley]] Professional |date=June 2005 |isbn=978-0-321-24678-3 |url=http://java.sun.com/docs/books/jls/index.html |accessdate=2008-12-03}}
*{{cite book |last=Flanagan |first=David |authorlink=David Flanagan |title=Java in a Nutshell |edition=Fifth |publisher=[[O'Reilly Media]] |date=March 2005 |isbn=978-0-596-00773-7 |url=https://archive.org/details/javainnutshell00flan |accessdate=2010-03-03 |url-access=registration}}
 
== References ==
* [[James Gosling|Gosling, James]]; [[Bill Joy|Joy, Bill]]; [[Guy Steele|Steele, Guy]]; & [[Gilad Bracha|Bracha, Gilad]] ([[2005]]). Java Language Specification, Third Edition. Addison-Wesley Professional. ISBN 0-321-24678-0. [http://java.sun.com/docs/books/jls/index.html online version]
 
{{reflistRefend}}
 
[[Category:Java (programming language)]]
[[Category:Programming language syntax|Java keywords]]