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 here }
char
char is a primitve 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 var2 = 44;
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 or 2"); }
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;
