The reserved keywords of the Java Language

This article will present you all the keywords of the Java Language and their purpose.

First of all, what's a reserved keyword ? It's a keyword of the language, for example true.

What does that change for the developer ? You cannot use one of there keywords to name a variable, a method, a class or a package. So you cannot have something like that :

Boolean true = new Boolean(true);

public class true{}

package com.wichtounet.true;

This example will not pass the compilation. But you can use it into a variable identifier or by changing the case :

Boolean trueBoolean = new Boolean(true);

public class TrueBoolean{}

package com.wichtounet.trueBoolean;

It's also really important for a developer to know all the keywords and to know how to use them.

Here are the meanings of all the keywords.

abstract

Modifier of class or method. This indicate that the class or method is abstract. A class must be declared abstract when one or more methods are abstract. An abstract class cannot be instanciated. You must create a subclass to use it. An abstract class can have abstract and normal methods.

An abstract method has no body and must be overriden by the subclass. An abstrac method cannot be final, static or private.

An interface is implicitely abstract like all its methods.

Here is a little example of an abstract class.

public abstract class Person{
    public abstract String getName(){}

    @Override
    public String toString(){
        return "My name is " + getName();
    }
}

Note that you can call an abstract method from a non-abstrac one.

assert

This keyword enable to guarantee a boolean condition before continuing execution. This is like contract programming. We often verify the methods parameters with an assertion.

An AssertionError is launched if the condition is not true. The assertions are disabled by default. You have to use -ea or --enableassertions options to enable them. An assertion must never edit the state of something, because they can be disabled.

private double divise(int a, int b){
    assert b != 0 : "Unable to divide by zero";
}

Note that this example is not very good, in practice, it's better for this kind of test to throw an IllegalArgumentException.

This keyword is present since java 1.4.

boolean

boolean is a primitive type of Java. It's a type indicating if something is true or false. This is the only two values than a boolean variable can take. This keyword can be used for the type of a variable, a parameter or the return of a method.

boolean test = true;

break

This keyword enable to go out of a control instruction, an iteration operator or a try block.

for(int i = 0; i < 1555; i++){
    if(condition){
        break; //We go out of the loop
    }
}

I't also often used to go out of a switch case statement :

switch(id) {
  case 1 : System.out.println("I'm first"); break;
  case 2 : System.out.println("I'm second"); break;
  case 3 : System.out.println("I'm third"); break;
  default : System.out.println("I don't know where i'm");
}

We can also use the break operation with a label to specify which statemetn we want to break. But it's a really bad practice.

byte

byte is primitive type of Java. It represent a signed integer of 8 bits. So it can take values from -128 to 127.

byte var = 27;

case

This keyword indicate a label of the switch control instruction. It's used to make an action for a specific case :

switch(id) {
  case 1 : System.out.println("I'm first"); break;
  case 2 : System.out.println("I'm second"); break;
  case 3 : System.out.println("I'm third"); break;
  default : System.out.println("I don't know where i'm !");
}

catch

This statement enable to catch an exception and to treat it. The catch block always follow a try block. If an exception is throwed inside of the try block, we arrive in the catch block and we can treat the exception.

try{
    //Something
} catch(AnException e){
    //If AnException is throwed we go
}

char

char is a primitive type of Java. It represent a character. Because character are only integer in Java, you can also see it like an unsigned integer of 16 bits from 0 to 65535. But if you do that, your code can quickly be hard to understand.

char var = 'a';

char var = 4;

class

This keyword declare a class. All declaration of a class must start with this keyword.

public class Test{
    //...

    private static class InnerClass {
        //..
        }
}

const

This keyword is reserved but not used.

continue

This keyword jump to the next iteration of a loop. So we go directly to the head (or the foot in the case of a do while) of the loop without executing the following instructions.

while(i > 100){
    if(i % 25 == 0){
        continue; //We go to the next iteration without executing "Others iterations"
    }

    //Others operations
}

default

This word is the default label of a switch case. The code affected to the default case will be executed if there is no case that have been executed or if the last case has not used break statement.

switch(i){
    case 1 :
        System.out.println("i equals 1");
        break;
    case 2 :
        System.out.println("i equals 2");
        break;
    default :
        System.out.println("i doesn't equals 1");
}

do

This keyword introduce a do... while loop. The stop condition of the loop is verified after the execution of the body of the loop. So the first iteration is always done.

do{
    //Operations
} while(condition);

double

double is a primitive type of Java. It's a floating-point number coded with 64 bits (8 octets). So it can take any value from 4.9E-324 to 1.7976931348623157E308. But take in memory than there is a loss of precision when you make double operations.

double var = 2.2335;

else

This keyword introduce the facultative part of the if condition operation. This part is executed if there is no other verified if condition.

if(i == 2){
    System.out.println("i equals 2");
} else {
    System.out.println("i doesn't equals 2");
}

enum

This keyword declare a enumeration. It seems a type who has only a finished set of values.

public enum Season {
   SPRING, SUMMER, FALL, WINTER;
}

This keyword is present since Java 5.0.

extends

This keyword is used to say than a class extends an other class or an interface extends an other interface.

public class Actor extends Person{ //Actor IS-A Person
    //...
}

false

This keyword is a value of the boolean type, it's the opposite of true.

boolean var = false;

final

This keyword is a modifier for method, variable or field or class :

In the declaration of a class, final indicate that this class cannot be extended.

In the declaration of a method, final indicates that this methods cannot be overriden (if the method is static or private, she's automatically final).

In the case of variable or field, final indicate that this reference is constant, we cannot change it anymore after declaration. In the case of objects, only the reference is constant, not the object. This is also useful to enable the usage of a variable in a anonymous class.

finally

Statement follow by an instructions block. This block must follow a try or try-catch block. Regardless of how we go out of the try block, the finally block is executed. It's often used to close resources after usages.

try {
    //Simple instructions
} finally {
    //Instructions always done after "Simple instructions"
}

float

float is a primitive type of Java. It's a floating-point number coded with 32 bits (4 octets). So it can take any value from 1.4E-45 to 3.4028235E38.

float variable = 4.44f;

for

This keyword introduce a for loop (while condition, we iterate, execute the for instruction). This loop has the particularity to allow instructions in its declaration. This declaration is made of 3 parts, separated by ;. The first is made from declare iteration variables, the second one is a condition and the third one can take any instruction.

for(int i = 0; i < 10; i++){
      //Operations
}

There is also an other kind of this loop, the foreach loop, introduced with Java 5.0. This extended for loop enabled to iterate on the content of a collection easily :

SimpleObject[] objects = new SimpleObject[x];

//...

for(SimpleObject object : objects){
    object.doSomething();
}

goto

This keyword is reserved but not used.

if

This operator enabled to define facultative block code. This block of instructions will only be executed if the condition is verified.

if(i == 1){ //Condition
    i = 3; //Only execute if (i == 1)
}

implements

This keyword is used in the declaration of a class to say that this class implements the functionalities of an interface. When a class implements an interface, she must define all the methods of the interface or be declared abstract.

public class JavaTester implements Tester{
    @Override
    public boolean test(){//Method defined in Tester
                //Do something
    }
}

import

This keyword allow to make a shortcut to the name of the name. So we cannot have to write the complete path to the path, but only the name of the class. It makes the code really clearer.

import com.test.Tester;

Tester.test();

If we doesn't import the class, we must write the complete path :

com.test.Tester.test();

From Java 5.0, we have also the import static who allow us to import directly the static methods and fields of an other class. This is practical to make the code clearer.

import static java.lang.Math.*;

public Class Test {
   public void calc (int i) {
      round(cos(i*(PI / 2)*E);
   }
}

instanceof

It's a condition instruction to verify than an object is of a certain type. If this object is of the asked type, the instruction will return true else false.

if(myObject instanceof Reader){
    System.out.println("myObject is of type Reader");
}

int

int is a primitive type of Java. It represent a signed integer of 32 bits (4 octets). So it can take values from -2'147'483'648 to 2'147'483'647.

int var = 3;

interface

This keyword declare an interface. An interface define a functionality or a behavior who are the same for some classes. An interface has no concrete code, all the methods are abstract. An interface can also have public static final constants.

public interface Tester(){

public void test();

}

long

int is a primitive type of Java. It represent a signed integer of 64 bits (8 octets). So it can take values from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807.

long var = 33;

native

This keyword is used in the declaration of a method to indicate than she's not coded in Java, but in a native language in a separated file. So this kind of method has no body because the code is not in Java.

public native void openCDRomTray();

new

This operator instanciate a new object of a certain class. It will create a new object and class and call the constructor of the class.

Object objet = new Object();

null

null is a special value indicating that the reference doesn't point on an object but on nothing. We say that this reference is null.

String str = null;

package

This keyword is used at the top of the class to indicate in which package is this class.

package com.mypackage;

public lass MyClass {

}

private

This keyword is used in the declaration of variabbles, fields, methods or classes. When we declare it private, this attributes are only accessibles by the class in which they are declared.

private int var = 11;

protected

This keyword is used in the decleration of field, methods or classes. When we declare it procted, this attributes are only accessibles by the sub classes and the classes of the same package.

protected int var = 11;

public

This keyword is used in the declaration of fields, methods or classes. When we declare it public, this attributes are acessible by all classes.

public int var = 11;

return

This method exit of the curren method. We can use it without parameter to go out of a void method or with a value to return something from the method. Of course, in a void method we cannot use return with a value and vice-versa.

public int getInt(){
    return 1;
}

public void doSomething(){
    return;
}

short

short is primitive type of Java. It represent a signed integer of 16 bits (2 octets). So it can take values from -32768 to 32767.

short var = -22565;

static

This keyword can be used in the declaration of a field, a method, a class or before a code block.

In a method, field or class declaration, static indicate that this member doesn't need an instance of the bounding class.

//Access to the static method abs of the Math class.

Math.abs(-1);

Of course, a static being independant of the instance, cannot access to instance variables and methods.

From a code block, we indicate that this block must be executed at the loading of the class.

public static ArrayList list = new ArrayList();

static {
    list.add(new String("x"));
    list.add(new String("y"));
}

strictfp

This keyword can be used in the declaration of a class, interface or method. It force the JVM to make calculation in accordance with the specification of the language so you have the guarantee that your calculation will have the same result regardless of the used VM.

This keyword is available from Java 1.2.

super

This keyword is a reference on the super class, it seems the class we extends.

public class Child extends Mother(){

public Child(){
    super(); //We call the constructor of the super class
}

@Override
public void myMethod(){
    super.myMethod();//We call the method myMethod of the super class
}

switch

This keyword introduce a control statement. This instruction test the value of an integer or an enum value and in function of the value execute the corresponding case. So we define some labels with the values we need to treat and the actions to execute in the specified case.

switch(value){
    case 1 :
        //Actions if value equals1
        break;
    case 2 :
        //Actions if value equals 2
        break;
    case default :
        //  in the other cases
        break;
}

synchronized

This keyword is used in concurrent programming in the declaration of a method or an instruction block to indicate that only one thread can access at the same time at this bloc or method.

public void synchronized method(){
    System.out.println("Two threads cannot acess this method at the same time");
}

This is almost equivalent to :

public void methode(){
    synchronized(this){
        System.out.println("Two threads cannot call this method at the same time");
    }
}

If the method is static, you can do like that :

public class MaClass {
    public static void methode() {
        synchronized(MaClass.class) {
            // code
        }
    }
}

You can also use an object as lock :

synchronized(monObjet) {
    // code
}

this

This keyword is a reference to the current object.

public MyClass(String attribute){
    super();
    this.attribute = attribute; //We take the attribute field of the class
    this.refreshAttributes();   //We class the refreshAttributes methods on the current object
}

// We can also call the constructors of the current class

public MyClass(){
    this("une valeur par défaut");
    //We call the constructor defined at the top of the code
}

throw

This instruction throw a new exception. The class of the exception must be in the hierarchy of Throwable. When an exception is launched, we go to the calling method or to the bouding catch block if there is one.

if(error)throw new Exception("There is an error");

throws

This keyword is used int the declaration of methods to indicate that this one can throw exceptions who are not caught.

public void read throws IOException {
    //Code that can throws an IOException
}

transient

This keyword enable to say than a field must not saved during the serialization of the class.

protected transient int variable = 2;

true

This keyword is a value of the boolean type, it's the opposite of false.

boolean variable = true;

try

This keyword introduce an instruction block. It doesn't have any other utility than allow the use of a catch and/or finally block.

try{
    //Instruction
} finally {
    //Instructions
}

void

This keyword declare a method with no return type. This method returns nothing.

public void methodThatReturnsNothing(){
    //Instructions diverses
}

volatile

We use this keyword on fields to force the VM to refresh it's value at each use. With that we are sure to tuse the real value and not the cached value. This is only useful in concurrent programming.

public volatile int var = 5;

while

This last keyword introduce a while loop. This loop will iterate while the condition is verified (true). The condition is verified before entering the loop, so it's possible to not enter in the loop if the condition is the condition is not verified.

while(condition){
    //Instructions
}

Related articles

  • Java Concurrency – Part 3 : Synchronization with intrinsic locks
  • Java 7 : More dynamics
  • Java Concurrency - Part 5 : Monitors (Locks and Conditions)
  • Java 7 : The new java.util.Objects class
  • Introduction to JR programming language
  • Better exception handling in Java 7 : Multicatch and final rethrow
  • Comments

    Comments powered by Disqus