Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

java language keywords

Thu Jan 29, 2009 9:31 pm

java language keywords :
Like any other programming language Java has a set of fifty reserved words named as "keywords" which are already in use for a specific predefined jobs created by Java designers. These keywords usually has a blue color in your programming IDE. Following is the list of Java keywords:
  • abstract
    Used to define abstract classes and abstract methods as follows :
    java code
    abstract class myLife{
    abstract void start();
    void end(){}
    }

  • assert
    Assert do checking on a specific condition, if true it passes, otherwise it throws an error. Assert is disabled by default you have to write : java -ea myJavaClass where you are going to run your application. Example of usage:
    java code
    int x=3;
    assert (x==3) : "X is" + x ;

    The message "X is" + x message is sent to error exception to be printed in the stack trace.
  • boolean
    Used to create a variable of the primitive data-type boolean. Boolean variables can take only two values true or false.
    java code
    boolean booleanVar=true;

  • break
    Used to stop the loops :
    java code
    for (int i = 4; i < 200; i++) {
    System.out.println("i="+i);
    if(i==10)
    break;
    }

    Also used in java switch to break after a case.
  • byte
    Used to create a primitive data of type byte.
    java code
    byte byteData;

  • case
    Used to create cases in java switch :
    java code
    int y=4;
    switch(y)
    {
    case 1:/* Do Something/*/ break;
    }

  • catch
    Is used to catch thrown exceptions:
    java code
    try {
    FileInputStream fileInputStream=new FileInputStream("file.txt");
    } catch (FileNotFoundException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

  • char
    Create a primitive variable of data type character.
    java code
    char ch='r';
  • class
    Used to declare classes even it is inner classes or outer classes.
    java code
    class outer
    {
    private int x;
    private double sum;
    private inner inerRef= new inner();
    public outer(){}
    private class inner
    {

    }
    }
    }
  • const
    const keyword is reserved but it has no function in java, you can just use the final keyword instead of it.
  • continue
    continue keyword is used to skip a current loop iteration.
    java code
    for (int j = 1; j < 10; j++) {

    if(j==5)
    {
    continue;
    }
    System.out.println("Print J"+j);
    }
  • default
    default keyword is used in java switch as else case if not match found in the other cases.
    java code
    int option=4;
    switch(option)
    {
    case 1:System.out.println("Case2"); break;
    case 2:System.out.println("Case2"); break;
    default: System.out.println("Default Case");
    }
  • do
    Is used to create a do-while for loop:
    java code
    int iterator=4;
    int count=0;
    do{
    count++;
    /// Do something.
    }while(count<iterator);

  • double
    Used to create a variable of primitive data type double of length 8 bytes(64bits)
    java code
    double x=4.3d;

  • else
    Used to create else case for if conditions.
    java code
    boolean varBol=false;
    if(varBol)
    {

    }else
    {

    }
  • enum
    Used to declare enum which is a list constant strings.
    java code
    enum HOLIDAYS {SUN,SAT};
  • extends
    If you want a java class to inherit from a another class you have to use extends as follows :
    java code
    class parent
    {
    }
    class child extends parent
    {
    }

  • final
    Declare variables as constants which means once initialized It can't be modified.
    java code
    class Math
    {
    public final float PI=3.144f;
    }

    You can also declare functions using final keyword which means these functions can't be overridden anymore.
    java code
    class shape
    {
    public void draw()
    {
    //Do something
    }
    }
    class circle extends shape
    {
    @Override
    public final void draw()
    {
    // Can't be overridden anymore.
    }
    }
  • finally
    finally is added to try-catch clause, finally clause always run after the try and its catches. Usually used to free resources.
    java code
    ServerSocket socket;
    try {
    socket = new ServerSocket(33);
    socket.accept();
    DataOutputStream remoteOut= new DataOutputStream(null);
    } catch (IOException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }finally{
    socket.close();
    socket=null;
    }
  • float
    Used to create a variable of type float of length 4bytes(32bits)
    java code
    float value=43.2f;
  • for
    Use to create a for loop.
    java code
    for(;;)
    {
    // infinite loop
    }
  • goto
    goto keyword is reserved in java so it can't be used in java. But Designers of java chose to reserve in hope for any future changes in design.
  • implements
    Used to allow classes to implement interfaces as follows :
    java code
    interface gun
    {
    public abstract void shot();
    }
    class shotgun implements gun
    {

    @Override
    public void shot() {
    // Fire Fire.
    }

    }
  • import
    Allows the loading of other classes at different packages to your class, by this way you can reuse defined classes. It should be added after package statement if there is a one.
    java code
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
  • instanceof
    Used to check if the a specific objects is an instance of a specific class or not.
    java code
    public class Main {

    public static void main(String[] args) {

    teams t1=new team1();
    if(t1 instanceof team1)
    {
    // t1 holds a team1 object.
    }else if(t1 instanceof team2)
    {
    // t1 hold a team2 object.
    }else
    {
    // Not known
    }


    }
    }
    interface teams
    {}
    class team1 implements teams
    {
    }
    class team2 implements teams
    {
    }
  • int
    Used to declare a integer variable with length four bytes(32 bits)
    java code
    int x=544;
  • interface
    Used to define an interface. Interface is a general description of behaviors and not a class.
    java code
    interface motor
    {
    }
  • long
    Used to create an integer variable with length 8bytes(64) bits.
  • native
    Java allows you to run functions implemented using other programming languages by marking them using native keyword.
    java code
    public native void printText (){
    }
  • new
    new keyword is used to invoke the creation of a new object of a specific class.
    java code
    List list=new ArrayList();
  • package
    Define the package path of the current class.
    java code
    package CurveIT;
    public class AmAClass {
    public static int limit=54;
    public static void setLimit(int newlimt)
    {

    limit=newlimt;
    }
    }
  • private
    The most restricted level of access providers in java allows the class members to be accessible only within the same class.
    java code
    class engine
    {
    private float powerHorse;
    private void Sleep(){}
    }
  • protected
    Access modifier that gives you average access where you can access class members within same class and at the child classes.
  • public
    You can access class members from anywhere using the public access modifier.
  • return
    return keyword used to return function output.
    java code
    public float sumArray(int[] arr)
    {
    float sum=0;
    for(int i=0;i<arr.length;i++)
    {
    sum+=arr[i];
    }
    return sum;
    }
  • short
    create a variable of primitive data-type short with length equal to two bytes(16bits).
    java code
    short x=2;
  • static
    Used to create static class members (functions and variables). Static members are shared between all the class instances.
    java code
    class calculator
    {
    public static final int numberOfOperations=4;
    public static void printOnScreen(double value)
    {
    // print on calculator screen,
    }
    }
  • strictfp
    The keyword strictfp is used to add more floating point arithmetic operations control. To avoid overflow and underflow in your floating calculations , you may need this in on your program , on some machines that doesn't provide large precision . in this case strictfp will be your solution .
  • super
    super is built in reference for the parent class.
    java code
    class book
    {
    protected String name;
    protected String serial;
    }
    class ScienceBook extends book
    {
    protected String serial;
    public void printSerial()
    {
    System.out.println("Super book"+super.serial);
    System.out.println("This book"+serial);
    }
    }
  • switch
    java allows you to create switch cases using the switch keyword as follows:
    java code
    String name="semo";
    switch(name)
    {
    case "semo": System.out.println("samz");break;
    case "stas": System.out.println("samz");break;
    case "tars": System.out.println("samz");break;
    }

  • synchronized
    synchronized keyword provide you with they ability to create a thread safe application. Only one thread can access part of code that is synchronized. You can synchronize functions and code blocks.
    java code
    class ball
    {
    private int size;
    public synchronized void hitBall()
    {

    }
    public void reset()
    {
    synchronized(this) {

    }
    }
    }
  • this
    this keyword is reserved as reference to the current object.
    java code
    class address 
    {
    private String street;
    private String postalCode;
    public address(String street,String postalCode)
    {
    this.street=street;
    this.postalCode=postalCode;
    }

    public String getStreet() {
    return this.street;
    }
    }

  • throw
    throw keyword is used to throw exception, you can throw exception in any case you want.
    java code
    public static void main(String[] args) {
    int x=3;
    if(x==3)
    {
    throw new IndexOutOfBoundsException();
    }
    }
  • throws
    If a function body contains a clause with has catch exceptions you can avoid using try/catch by marking the function throws the caught exceptions.
    java code
    class door
    {
    public void openDoor() throws FileNotFoundException
    {
    File file= new File("path");
    BufferedReader bufferedReader=new BufferedReader(new FileReader(file));

    }
    }
  • transient
    In hope to avoid serializing an specific class members you can use transient keyword. Serializable objects could be converted to xml and sent using IO streams (RMI).
    java code
    class book implements Serializable
    {
    private String content;
    private transient int pages;
    }
  • try
    Used to handle exception by try/catch clause.
  • void
    void means that no return value from a function.
  • volatile
    Used to mark class variables that can be changed asynchronously using threads.
    java code
    class pen  
    {
    private String color;
    private volatile String price;
    }
  • while
    Used to create the default well known while loop.
    java code
    while(true)
    {
    break;
    }
These words are reserved as a java keywords .



Re: java language keywords

Mon Jun 17, 2013 1:44 am

updated.

Post a reply
  Related Posts  to : java language keywords
 in what language is C++ and Java are written?     -  
 SEO Keywords Suggestion     -  
 keywords and description of HTML page using meta tag     -  
 what is c++ a brief to c++ language!!!     -  
 Window Through C Language     -  
 How do you know when to use the right programming language?     -  
 I need you to help me to solve this program by c++ language     -  
 Which language is required to set up a web application?     -  
 Mark-up Language Differences     -  
 Elements of Assembly Language Instruction     -  

Topic Tags

Java Basics