Here is a list of the keywords of the JavaTM programming language, which cannot be used as variable names:
Of the set below, the following are all the primitive types of the Java language:
Java Type: | boolean | byte | char | double | float | int | long | short |
Size in Bytes: | 1 | 1 | 2 | 8 | 4 | 4 | 8 | 2 |
These are explained among the following keywords: Note that they are all lowercase. Java is case sensitive, and all the keywords in Java must be lowercase. Variable names may be of any case, however.
abstract | Used in declartion of classes and methods in classes declared abstract. An astract method has no implementation, as in an interface, and any class that has an abstract method must be declared abstract. It is intended to be extended by child classes that implement the abstract methods. |
assert |
a new keyword with java version 1.4. Used for debugging, by default it is ignored
unless the java command is like:
[java -enableassertions MyClassName] or [java -ea MyClassName]. When it is used as in the above command, then when a process encounters the keyword assert in the form as follows: assert([boolean expression]); example: assert(a+b>c); the expression inbetween the parentheses will be evaluated and if the expression evaluates to false, it prints a diagnostic message and aborts the program. However, if expression evaluates to true, then nothing happens and the program continues on. This is similar to how assert is used in C and C++ except the expression must evaluate to a boolean type, not a number. see assert keyword explained |
boolean | The primitive type that holds either a true or false value. Note, this value can not be converted to a number, as is the case with other languages. |
break |
A control flow command that breaks out of for, do while, while loops and switch sequences.
Can be used with labels. Example:
int i = 0; OUTER: while(true){ MYINNERFOR: for( ; i < 10; ++i){ if (i == 7) break OUTER; if ( i == 4) break MYINNERFOR; } System.out.println("Inside i: "+i); } System.out.println("Outside i: "+i); //this prints: Inside i: 4 Outside i: 7If a label is not provided, break only breaks out of the innermost loop.\ |
byte | The primitive type for a 8-bit signed integer. |
case | A control flow method used in switch commands to test a value for executing a block of code. See switch. |
catch | An Exception handling command to catch Exceptions in a try catch block. See try. |
char | The primitive type for a 16-bit unsigned integer (The only unsigned number in Java). It represents a single Unicode character. |
class | Used to declare a module of code that is a class. An Object is an instance of a class. A class may extend from one super class and may implement multiple interfaces, and unless it is declared final, it may be extended by other classes. Object is the default super class and the ultimate root of the entire class hierarchy and hence has no super class. |
const | Unused. Java reserves this word for possible future use. |
continue |
A control flow command that is used to immediately jump to the next iteration of
a for, while, or do while loop. Example:
MYFOR: for(int i = 0; i <= 5; ++i){ if (i < 4) continue MYFOR; System.out.println(""+i); } //this prints: 4 5 |
default | The control flow command that is used to signify the default of a switch command. See switch. |
do | A control flow command used to construct a loop that executes at least once, and until the condition in the while expression at the end of the block evaluates to false. |
double | The primitive type for a 64-bit real number. |
else | A control flow command that defines a block to be executed as an alternative to an if block or a previous else block. |
enum |
a new keyword with java version 1.5. An enum is a code module derived from
the class construct
and like a class it can contian its own methods and variables. Here is enum
used to represent three types of a CoinColor:
public enum CoinColor { copper, nickel, silver }Note its similarity to class in that it ends without a semicolon. An example of a enum with methods is as follows: public enum Coin { penny(1), nickel(5), dime(10), quarter(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } }An example of using enums is as follows: public CoinColor getCoinColor(Coin c) { switch(c) { case Coin.penny: return CoinColor.copper; case Coin.nickel: return CoinColor.nickel; case Coin.dime: case Coin.quarter: return CoinColor.silver; default: throw new RuntimeException("Unknown coin: " + c); } } public void showAllCoins(){ for (Coin mycoin : Coin.VALUES){ System.out.println("My coin is: "+mycoin.toString()); System.out.println("My coin's value is: "+mycoin.value()); CoinColor mycoincolor = getCoinColor(mycoin); System.out.println("My coin's color is: "+mycoincolor.toString()); } } |
extends | Used in class declaration to specify the name of the superclass other than Object. Object is the absolute root superclass of all java classes and classes that don't specify a superclass have Object as the implied superclass. |
false | One of the two possible values for the boolean primitive type. |
final | Used in declarations to describe classes (cannnot be extended), methods (cannot be overridden), and variables (having fixed and unalterable values). |
finally | An exception handling command that describes a block of code that is guaranteed to always executed at the end of a try/catch sequence. Used to guarantee resources are closed that are opened within a try/catch sequence by putting the resource closing code within the finally block. See try. |
float | The primitive data type for a 32-bit real number. |
for |
A control flow command that defines a loop as such:
for(initializing; boolean condition; increments){...} example: for (int i=0; i < 10; i++){ System.out.println("count="+i); } |
goto | Unused and reserved in Java for possible future use. |
if | A control flow statement that evaluates a boolean condition to determine whether to execute a block of code. |
implements | Used in a class declaration to declare implementation of one or more interfaces. |
import | Used at the top of the java source file to indicate where the classes it uses are located from the ClassPath. Example: import java.util.*; Note: import java.lang.*; is implied in every class and does not need to be written. |
instanceof | The only keyword that is also an operator. It takes an Object instance on its left and a class type on its right and evaluates to true if the Object is an instance of the type. |
int | The primitive data type for a 32-bit signed integer. |
interface | The declaration of a module of code that is an alternative to class. In an interface, all fields are public final static and all methods are without implementation, to be implemented by the class that is to be of that interface type. |
long | The primitive type that is a 64-bit signed integer. |
native | Used to designate a method declaration where the method is implemented outside Java in machine-native code. |
new | Used to instantiate new objects, allocating memory for them. |
null | The value an Object that indicates it refers to nothing. |
package | Must be the topmost item in a java source file to indicate the package the class belongs to. If absent, it signifies the default package directly pointed to in the ClassPath or the current directory where the class must be to be used by another class. If a package is present, the class files must be located in the directory structure described by the package stemming off from the directories pointed to by the ClassPath. |
private | Used to apply to methods and fields to limit their scope to within that class only and not to any other classes, not even to descendents. |
protected | Used to apply to methods and fields to limit their scope to within that class and to descendents, but not to any outside classes. |
public | Used to apply to methods and fields to make them accessable to all classes. If used to apply to a class, only one class can be made public in a java source file. |
return | A flow control statement to exit from a method. If the method has a return value (is not void) the return command must be followed by a value of the return type specified in the method declaration. |
short | The primitive data type that is a 16-bit signed integer. |
static |
Used to declare methods and fields as being part of the class and not of the generated
instances, although the instances have access to them. Static methods cannnot access
instance data or methods and can be called without making an instance of the class
in which they are declared.
A static class is a class declared as a member of another class or interface, but with the static modifier, allowing it to be instantiated independently from the context of an object of the containing class. A static class is also called a nested top-level class, and is used as a convenient way to group related classes without making a new package. |
super | Used to address data and methods in the parent class. |
switch |
A flow control statment that is used with case and break statements to select a single
block of code for execution: Example:
switch (primitive expression){ case 1: DoSomeCode(); break; case 2: DoSomethingElse(); break; default: DoThis(); break; } |
synchronized | Used to lock Object instance methods or Objects or static methods to allow only one Thread to operate in that marked code at any one time. |
this | Refers to the current object containing the executing code. |
throw | Used to throw an Exception out of the block of code possibly to be caught by a catch statement following the try block in which the exception occurred. |
throws | Used to specify Exceptions that are thrown in a method declaration. |
transient | Used in serializing objects to declare Objects that cannot be serialized and should be ignored by the serializing process. |
true | One of the two possible values for the boolean primitive type. |
try |
Used in Exception handling to execute code that may throw exceptions.
Example:
try{ DoMyCode(); } catch(Exception ex){ handleMyEx(); } finally{ closeMyResources(); } |
void | Used in method declarations to signify that the method does not return a value. |
volatile | Used to declare a variable as possibly changing unexpectedly from an external source. |
while |
A flow control that repeats a loop for as long as its boolean condition is true.
Example:
while (a > b){ b++; doSomething(); } |
*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*