#REDIRECT [[Java syntax#Keywords]]
[[Image:Java keywords highlighted.png|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 one of 50 [[reserved word]]s which have a predefined meaning in the language; because of this, programmers cannot use keywords 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 Keywords |url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html |work=The Java Tutorials |publisher=[[Sun Microsystems, Inc.]] |date=February 14, 2008 |accessdate=2008-12-02}}</ref> Due to their special functions in the language, most [[integrated development environment]]s for Java use [[syntax highlighting]] to display keywords in a different color for easy identification.{{Fact|date=December 2008}}
The following is a list of the keywords in Java, along with brief descriptions of their functions:<ref name="keywords" />
;<code>[[Abstract class|abstract]]</code>
:The <code>abstract</code> keyword is used to declare a class or method to be [[abstract type|abstract]]. An abstract method has no implementation; all classes containing abstract methods must themselves be abstract, although not all abstract classes have abstract methods. [[object (computer science)|Object]]s of a class which is abstract cannot be [[object (computer science)|instantiated]], but can be [[inheritance (computer science)|extended]] by other classes. All [[subclass (computer science)|subclasses]] of an abstract class must either provide implementations for all abstract methods, or must also be abstract.<ref name="abstract">{{cite web |title=Abstract Methods and Classes |url=http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-06}}</ref>
;<code>[[Assertion (computing)|assert]]</code>
:The <code>assert</code> keyword, which was added in [[Java version history#J2SE 1.4 (February 6, 2002) (Unsupported)|J2SE 1.4]],<ref name="keywords" /> is used to make an [[assertion (computing)|assertion]]—a statement which the programmer believes is always true at that point in the program. If assertions are enabled when the program is run and it turns out that an assertion is false, an {{Javadoc:SE|java/lang|AssertionError}} is thrown and the program terminates. This keyword is inteded to aid in [[debugging]].<ref name="assert">{{cite web |title=Programming With Assertions |url=http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html |work=Java 2 SDK, Standard Edition Documentation |publisher=Sun Microsystems, Inc. |date=2002 |accessdate=2008-12-06}}</ref>
;<code>[[Boolean datatype#Java|boolean]]</code>
:The <code>boolean</code> keyword is used to declare a [[field (computer science)|field]] that can store a [[boolean datatype|boolean value]]; that is, either <code>true</code> or <code>false</code>.<ref name="primitive">{{cite web |title=Primitive Data Types |url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref> This keyword is also used to declare that a method returns a value of type <code>boolean</code>.<ref name="return">{{cite web |title=Returning a Value from a Method |url=http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
;<code>[[Switch statement#Java|break]]</code>
: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>[[byte]]</code>
:The <code>byte</code> keyword is used to declare a field that can store an 8-[[bit]] signed [[two's complement]] integer.<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>byte</code>.<ref name="return" />
;<code id="case">[[Switch statement#Java|case]]</code>
:The <code>case</code> keyword is used to create individual cases in a [[switch statement]]; see ''<code>[[#switch|switch]]</code>''.<ref name="switch">{{cite web |title=The switch Statement |url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
;<code>[[Exception handling syntax#Java|catch]]</code>
:Defines an [[exception handler]]—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>[[Character (computing)|char]]</code>
:The <code>char</code> keyword is used to declare a field that can store a 16-bit [[Unicode]] character.<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>char</code>.<ref name="return" />
;<code>[[Class (computer science)#Java|class]]</code>
: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>[[Variable#Constant|const]]</code>
:Although reserved as a keyword in Java, <code>const</code> is not used and has no function.<ref name="keywords" />
;<code>[[continue (Java)|continue]]</code>
: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 id="default">[[Switch statement#Java|default]]</code>
:The <code>default</code> 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" />
;<code id="do">[[do while loop#Java|do]]</code>
: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=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
;<code>[[Double precision|double]]</code>
:The <code>double</code> keyword is used to declare a field that can hold a 64-bit [[double precision]] [[IEEE 754]] [[floating-point number]].<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>double</code>.<ref name="return" />
;<code id="else">[[Conditional statement|else]]</code>
: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=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
;<code>[[Enumerated type|enum]]</code> (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>[[Inheritance (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. All methods in a final class are implicitly <code>final</code>.
;<code>[[Exception 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>[[single precision|float]]</code>
:The <code>float</code> keyword is used to declare a field that can hold a 32-bit [[single precision]] IEEE 754 floating-point number.<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>float</code>.<ref name="return" />
;<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=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
:As of [[Java version history#J2SE 5.0 (September 30, 2004) (EOL)|J2SE 5.0]],{{Fact|date=December 2008}} the <code>for</code> keyword can also be used to create a so-called "[[for each loop|enhanced for loop]]", which specifies an [[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" />
;<code>[[GOTO|goto]]</code>
:Although reserved as a keyword in Java, <code>goto</code> is not used and has no function.<ref name="keywords" />
;<code id="if">[[if statement|if]]</code>
: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" />
;<code>implements</code>
:Included in a class declaration to specify one or more [[Interface (Java)|interfaces]] that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces.
;<code>import</code>
: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.
;<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>[[Integer (computer science)|int]]</code>
:The <code>int</code> keyword is used to declare a field that can hold a 32-bit signed two's complement integer.<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>int</code>.<ref name="return" />
;<code>[[Interface (Java)|interface]]</code>
:Used to declare a special type of class that only contains abstract 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.
;<code>[[long integer|long]]</code>
:The <code>long</code> keyword is used to declare a field that can hold a 64-bit signed two's complement integer.<ref name="primitive" /> This keyword is also used to declare that a method returns a value of type <code>long</code>.<ref name="return" />
;<code>native</code>
:Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.
;<code>[[Object lifetime#Java|new]]</code>
:Used to create an instance of a class or array.
;<code>[[Java package|package]]</code>
:A group of types. Packages are declared with the <code>package</code> keyword.
;<code>[[Java_syntax#Class_member_access|private]]</code>
: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=http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html |work=The Java Tutorials |publisher=Sun Microsystems, Inc. |date=February 14, 2008 |accessdate=2008-12-03}}</ref>
;<code>[[Java_syntax#Class_member_access|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 ther own class or that class's [[inheritance (object-oriented programming)|subclasses]].<ref name="access" />
;<code>[[Java_syntax#Class_member_access|public]]</code>
:The <code>public</code> 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.<ref name="access" />
;<code>[[Method (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" /> This keyword is also used to declare that a method returns a value of type <code>short</code>.<ref name="return" />
;<code>[[Static 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 [[Name 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 actually top-level classes and are ''not'' inner classes.)
;<code>[[strictfp]]</code> (as of J2SE 1.2)
:A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability.
;<code>[[Inheritance (object-oriented programming)#Java|super]]</code>
: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 id="switch">[[Switch statement#Java|switch]]</code>
: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>, 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" />
;<code>[[Mutual exclusion|synchronized]]</code>
: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. For static methods, the object locked is the class' <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>[[Exception 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>[[Exception 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 [[Serialization|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.
;<code>[[Exception 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>
:The <code>void</code> keyword is used to declare that a method does not return any value.<ref name="return" />
:This keyword is also used as a nonfunctional statement.{{Fact|date=December 2008}}
;<code>[[Volatile variable|volatile]]</code>
:Used in field declarations to specify that the variable is modified [[asynchronous]]ly by concurrently running threads. Methods, classes and interfaces thus cannot be declared ''volatile''.
;<code id="while">[[do while loop#Java|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|do]]</code>''.<ref name="do-while" />
==[[Reserved word]]s for [[literal]] values==
;<code>[[Truth value|false]]</code>
:A boolean literal value.
;<code>[[null (computer programming)|null]]</code>
:A reference literal value.
;<code>[[Truth value|true]]</code>
:A boolean literal value.
== References ==
;General
*{{cite book |last=Gosling |first=James |authorlink=James Gosling |coauthors=[[Bill Joy|Joy Bill]]; [[Guy Steele|Steele, Guy]]; and [[Gilad Bracha|Bracha, Gillad]] |title=Java Language Specification |edition=3rd |publisher=Addison-Wesley Professional |year=2005 |url=http://java.sun.com/docs/books/jls/index.html |accessdate=2008-12-03}}
;Footnotes
{{reflist|2}}
[[Category:Java programming language]]
|