Friday, August 15, 2014

Java Keywords

Following is the list of Java keywords . These keywords cannot be used as names for variable, classes or methods.All keywords are written in lower-case. Java is a case sensitive language so the keywords can only be written in lower case.
abstractbooleanbreakbytecase
catchcharclasscontinuedefault
dodoubleelseextendsfinally
intpublicthisfloatinterface
returnthrowforlongshort
throwsnativestatictransientif
newtruetryimplementspackage
supervoidimportprivateswitch
volatilefinalinstanceofprotectedsynchronized
whilefalsenullassertenum
These keywords are reserved for future use.
const
goto
abstract
This is used to qualify a class. When a class is written as abstract, a object of that class can not be instantiated.
abstract classs Foo{
}
boolean
It take either "true" or "false". we can't convert it into numeric values.
boolean status = true;
break
It terminate the loop.
while{
  //code here
   break;
}
byte
A byte can store an integer value in the range [−128, 127].
byte b = 124;
case
The case is used to label each branch in a switch statement.
int arg = <some value>;
switch (arg)
{
   case 1: {
      <statements>
      break;
     }
   case 2:{
      <statements>
      break;
   }
   default: {
   <statements>
   break;
  }
}
catch
The catch keyword is used to define exception handling blocks in try−catch statements.
try{
   <block that may throw exceptions>
  }catch (<java.lang.Exception or subclass> e)
  {
     <code to handle exception e>
  }
char
A char variable can store a single  character.
char  d = 'a';
class
It is used to declare a new Java class, which is a collection of related variables and methods.
To use a class, we instantiate an object of the class.
class Test
{
}
 continue
The continue keyword is used to skip to the next iteration of a for, while, or do loop.
for (i=0; i<max; i++)
{
   <statements>
   if (<done with this iteration>)
   {
      continue;
   }
   <statements>
}
 default
The default keyword is used to label the default branch in a switch statement.
Examples
int arg = <some value>;
switch (arg)
{
   case 1:
      <statements>
      break;
   case 2:
      <statements>
      break;
   default:
     <statements>
      break;
}
do
The do keyword specifies a loop whose condition is checked at the end of each iteration.· The body of a do loop is always executed at least once.
do
{
   <statements>
}
while (not found);
 double
It store a double−precision floating point value.
double diameter = 6.15;
double height = 1.35E03; // 1.35 * 103 or 1350.0
double height = 1e−2; // 1.0 * 10−2 or 0.01
 else
The else keyword is always used with the 'if' keyword, in an 'if−else' statement. The else clause is optional and is executed if the 'if' condition is false.
if (condition)
{
   <statements>
}
else
{
   <statements>
}
 extends
The extends keyword is used ,when one class inherits the features of another class.
public class Rectangle extends Polygon
{
   //the Rectangle class inherits all of the public and protected variables and methods of the Polygon class.
}
 finally
The finally keyword is used to define a block that is always executed in a try−catch−finally statement.
try
{
   <block that may throw exceptions>
}catch (<java.lang.Exception or subclass> e)
{
   <code to handle exception e>
}finally
{
   <statements that execute with or without exception>
}
 int
A int variable may store a 32−bit integer value.
int  num = 5;
int  octalNumber = 0377;
int  hexNumber = 0xff;
 public
This keyword is an ''access control modifier ''that may be applied to a class,a method or a variable.
public class MyPublicClass{
    public int i;
    public String myMethod()
    {
       <statements>
    }
}
this
The this keyword refers to the current instance.
public class Hello
{
   int number;
   public Hello(int number)
   {
      this.number = number;
   }
}
 float
A float variable may store a single−precision floating point value.
float diameter = 6.15F;
float height = 1.35E03f; // 1.35 * 103 or 1350.0
 interface
The interface keyword is used to declare a new Java interface, which is a collection of methods. Any class that implements an interface must provide implementations for all methods in that interface.
public interface Polygon
{
   public float getArea();
   public int getNumberOfSides();
   public int getCircumference();
}
return
This keyword return a value  that matches the
return type of the returning method.
public void Method()
{
   <statements>
   return;
}
public String stringMethod()
{
   String s = "my response";
   return s;
}
throw
The throw keyword is used to raise an exception.
public class MyExceptionClass
{
   public method readFile(String filename) throws IOException
   {
       <statements>
      if (error)
      {
         throw new IOException("error reading file");
      }
   }
}
for
The 'for' keyword specifies a loop whose condition is checked before each iteration.
int i;
for(i=0; i<max; i++)
{
   <statements>
}
long
A long variable may store a 64−bit signed integer.
long Number = 34590L;
short
A short variable may store a 16−bit signed integer.
short number = 5;
throws
It used to raise a particular type of exceptions.
public class MyClass
{
   public method readFile(String filename) throws IOException
   {
      <statements>
      if (error)
      {
          throw new IOException("error reading file");
      }
   }
}
native
This keyword may be applied to a method to indicate that the method is implemented in a language other then Java.
native String getProcessorType();
static
The static keyword may be applied to an inner class (a class defined within another class), method or field.
public class AnyClass
{
   public final static int MAX_OBJECTS = 100;
   static  int _numObjects = 0;
   static  class MyStaticClass
   {
   }
   static int getNumObjects()
   {
   }
}
transient
The transient keyword may be applied to member variables of a class to indicate that the member variable should not be serialized when the containing class instance is serialized.
public class MyClass
{
   private transient String password;
}
if
The if keyword indicates conditional execution of a block. The condition must evaluate to a boolean value.
if (condition)
{
   <statements>
}
new
The new keyword is used to create a new instance of a class.
String sName = new String();
Float fVal =new Float(0.15);
true
The true keyword represents one of the two legal values for a boolean variable.
boolean isComplete = true;
try
It can have one or more statement that could generate an exception.
try{
   <block that may throw exceptions>
}catch (<java.lang.Exception or subclass> e)
{
   <code to handle exception e>
}
implements
The implements keyword is used in a class declaration to indicate that the class being declared provides implementations for all methods declared in the interface whose name follows the implements keyword.
public class Truck implements IVehicle
{
//the Truck class must provide implementations for all methods declared in the IVehicle interface.
}
package
It is a collection of classes.
package com.mycompany;
public class MyClass
{
}
super
The super keyword refers to the superclass of the class in which the keyword is used.
public class MyClass
{
   public MyClass(String arg)
   {
      super(arg);
   }
}
void
The void keyword represents a null type.
public class MyClass
{
   public void doSomething();
   {
       <statements>
       return;
   }
}
import
The import keyword makes one class or all classes in a package visible in the current Java source file.
import java.io.File;
private
The private keyword is an access control modifier that may be applied to a class, a method or  variable declared in a class.
public class MyPublicClass
{
    private class MyPrivateClass
   {
   }
   private int i;
   private String myMethod()
   {
      <statements>
   }
}
switch
The switch statement is used to select execution of one of multiple code blocks based on an expression.
int arg = <some value>;
switch(arg)
{
   case 1:
      <statements>
      break;
   case 2:
      <statements>
      break;
   default:
      <statements>
      break;
}
volatile
The volatile keyword may be used to indicate a member variable that may be modified asynchronously by more than one thread. volatile is intended to guarantee that all threads see the same value of the specified variable
Note: the volatile keyword is not implemented in many Java Virtual Machines.
public class MyClass
{
  volatile int sharedValue;
}
final
The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
The final keyword may be applied to a method, indicating the method may not be overridden in any subclass.
public final class MyFinalClass
{
}
public class MyClass
{
   public final  String myFinalMethod()
   {
   <statements>
   }
}
instance of
The instanceof keyword is used to determine the class of an object.
if (apple instanceof Fruitclass)
{
   <statements>
}
protected
The protected keyword is an ''access control modifier ''that may be applied to a class, a method or  a variable declared in a class.
public class MyPublicClass
{
   protected class MyPrivateClass
   {
   }
   protected  int i;
   protected String myMethod()
   {
      <statements>
   }
}
synchronized
The synchronized keyword may be applied to a method or statement block and provides protection for getting simultaneously accessed by more than one thread.
public class MyClass
{
   public synchronized static String mySyncStaticMethod()
   {
      //When applied to a static method, as with MySyncStaticMethod in the examples , the   
      //entire class is locked while the method is being executed by one thread at a time.
   }
   public synchronized String mySyncMethod()
   {
       //When applied to an instance method, as with MySyncMethod in the examples,
       // the instance is locked while being accessed by one thread at at time.
   }
}
while
The while keyword specifies a loop that is repeated as long as a condition is true.
while (!found)
{
   <statements>
}
null
null is a Java reserved word representing no value.
Integer i;
i = null;
String s;
if (s != null)
{
   <statements>
}
false
The false keyword represents one of the two legal values for a boolean variable.
boolean isComplete = false

More on Java

2 comments:

  1. I am really curious to know where did you get that list of future reserved keywords from ? as I cannot find any other source citing outer, inner or cast as future keywords.

    ReplyDelete
  2. Nicolas,

    Good catch. I did a little research to figure out this as I had written this post sometime back (The date shown is more latest as I had move this post from another link). It seems these were reserved keywords in some older version of Java. I could find one reference at http://web.deu.edu.tr/doc/oreily/java/javanut/ch13_05.htm#JNUT2-CH-13-TAB-6

    Now I have updated the post to reflect the Java 8 keywords as shown in http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

    ReplyDelete