HomeJava

50 keywords in Java you should be aware of

50 keywords in Java you should be aware of
Like Tweet Pin it Share Share Email

Every programming language will have a huge list of keywords so that the respective compiler or interpreter can understand to perform the actions accordingly, similarly we have 50 keywords in Java and it is very important that you should know to write programs in java.

Most of you fear its a huge list, but by just giving a quick read it will make your life easier to start writing programs in java, you need not know exact purpose and meaning of all 50 java keywords at once, you will learn them as you practise writing more and more programs in java.

These keywords can be categorised on different usage purposes and the list goes below:

  • Data Types
  • Access modifiers
  • Non Access modifiers
  • Control structures / Flow structures related
  • Error Handling
  • Object Oriented Programming related
  • Others / Miscellaneous
  • Un Used

50 Keywords in Java are explained below for each category with purpose

8 Keywords in Data Types category

Data Types are the different types of data that java can hold and can be represented in memory, each primitive data type has its own capacity of data that it can hold.

Below list of keywords are used to  either declare a variable for a specific type supported in java or as a return type to methods in java.

8 keywords in primitive data types of java

S.No Keyword Type Purpose
1 int Primitive A data type which can hold ( 32 bit / 4 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.

ex: int i = 10;

2 boolean Primitive A data type which can hold only 2 values, i.e. true and false, can be used directly in control structures like if, while, for, etc..

ex: boolean flag = true;

3 long Primitive A data type which can hold ( 64 bit / 8 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.

ex: long value = 10;

4 double Primitive A data type which can hold ( 64 bit / 8 bytes ) numeric values with double precision (decimals), i.e. both negative and positive numbers with decimal points.
5 char Primitive A data type which can hold character supported by java character set in 1 byte of memory.

ex: char alpha = ‘a’;

6 byte Primitive A datatype which can hold any data within 1 byte of memory.
7 short Primitive A data type which can hold ( 16 bit / 2 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.

ex short value = 1;

8 float Primitive A data type which can hold ( 32 bit / 4 bytes ) numeric values with single precision (decimals), i.e. both negative and positive numbers with decimal points.

ex: float value = 10.15;

3 Keywords in Access modifiers category

Access modifiers are the ones which regulate the access of class members in different locations of programming, like following:

  • Accessibility in same class
  • Accessibility in sub classes
  • Accessibility in classes in same package
  • Accessibility in classes in other packages


Source & Credits: JIRA

Following are the list of keywords you can use to declare a specific class member (i.e. a method or a field or an inner class) and regulate the accessibility of them.

S.No Keyword Purpose
9 private Class members declared with private keyword in a class can be accessible only by the members of the same class.
10 public Class members declared with public keyword in a class can be accessed by the members of the same class or a sub class or any class in same package or even in a class of a different package.
11 protected Class members declared with protected keyword in a class can be accessed by the  members of the same class or a sub class or any class present in same package.

6 Keywords in Non Access modifiers category

These are the list of keywords which regulate the behaviour of class members when declared with them.

S.No Keyword Purpose
12 static static keyword is used when you want a property(variable/attribute), method to be accessible even without creating an object for a class.  The non static property gets a separate memory allocated whenever a new object is created, but for static it has all the time. that is it can be accessed even when object is not created.

public class Car
{
    public int acLevel;
    public static int carCount;
    public static void main(String rs[])
    {
        // carCount property is accessed directly with 
        //class name as it is declared as static. 
        Car.carCount = 0;
        // but if you have to access acLevel, 
        /// you need to create an object, only then you can access
        Car c = new Car();
        c.acLevel = 4;
    }
}
13 final “final” keyword in java is used for multiple purposes, especially if you want to have only one time assignment we can use final keyword. For example, if you want to assign a value to a variable only once like a constant we can use final keyword.

final int acLevel = 4;

Similarly

  • if you use final keyword for a method, it cannot be overridden
  • if you use final keyword to a class, we cannot extend that class.
14 volatile volatile keyword is not much used and need not required to learn deep about it, when you are trying to learn java for test automation only.

But still let us understand a brief meaning about volatile: When a variable/attribute is declared with this attribute, the values are stored directly in main memory and it never gets cached especially during the read / write actions.

15 transient This is not much required to learn, when you are trying to learn java for test automation only. you can search in google if you are still interested
16 abstract Abstract keyword is used for a class where you want to just create a template for your subclass, and we cannot create an object for an abstract class. When we declare an abstract class, it is mandatory that at least one method in class is abstract, that is it will not have a definition but just a declaration of a method.

We may not use much of this concept in test automation, you can explore more in google if you want to learn deep about it.

17 synchronized Synchronized is generally used when we want to work with threads and is an advanced concept. We may not use much of this concept in test automation, you can explore more in google if you want to learn deep about it.

11 Keywords in Control / Flow structure category

These are the keywords which will determine the code flow, i.e. if you want to repeat some specific set of statements or if you want certain set of statements to be executed or skipped based on conditions of the business logic that is written in the program.

S.No Keyword Type
18 if Conditional Construct
19 else Conditional Construct
20 while Looping construct
21 for  Looping Construct
22 do  Looping Construct
23 break Jumping statement – generally used with loops in conjunction with conditional constructs
24 continue Jumping statement – generally used with loops in conjunction with conditional constructs
25 return Jumping statement – generally used with conditional constructs & functions
26 switch  Conditional Construct
27 default Jumping statement – generally used with conditional construct Switch
28 case Jumping statement – generally used with conditional construct Switch

In order to dwell more in to each of the above conditional & looping constructs I would recommend you to read the article – “All about Conditional and looping constructs in Java”

6 Keywords in exception handling category

S.No Keyword Purpose
29 try try keyword is used when you want to handle exception handling, i.e. at any time if you are suspicious that certain set of commands can raise an execution at run time.

We can include those statements in side try block, so that when ever a run exception is raised, instead of stopping the execution it will jump the control to corresponding catch block.

try
{
    int a = 0;
    // below statement raises divide by zero exception
    // or an arithmetic exception.
    int b = 10/a; 
}
catch(ArithmeticException ae)
{
    System.out.println("Arithmetic Exception raised");
}
catch(Exception e)
{
    System.out.println("Unknown Exception is raised");
}

Single try block can have single or multiple catch blocks, i.e. we can catch different kinds of exceptions by specifying them individually and handle them separately as per your needs. Exceptions like FileNotFoundException, ClassNotFoundException, IOException, etc..

30 catch You might have understood by now the purpose of catch keyword in above example.
31 throw
32 throws
33 finally
34 assert

7 Keywords in Object Oriented Programming category

S.No Keyword Purpose
35 class This keyword is used to declare a class

// keyword class is used to declare a class in 
// java programming language. 
public class Car
{
}
36 implements This keyword is used if you want to implement an interface, i.e. if a sub class wants to implement parent class( i.e. interface), This is mainly used to achieve inheritance concept of Object oriented programming
37 extends This keyword is used if you want to extend a class or an interface. This is mainly used to achieve inheritance concept of Object oriented programming
38 interface A class in which you just don’t want to have any method to be defined but just be declared.
39 new new keyword in java is used to create an object for a class.

public class Car
{
    public int acLevel;
    public static void main(String rs[])
    {
        // new keyword is used to create an object
        Car c = new Car();
        c.acLevel  4;
    }
}
40 super
41 this

7 Keywords in Miscellaneous category

S.No Keyword Purpose
42 strictfp we may not use this much in Test Automation, but basically it is used to declare a method or class a to restrict all its attributes to have non decimal values.
43 import this keyword is used especially when you want to include some libraries or jar files.

Java has a huge set of packages already developed for developers, if you want to use those classes from those packages, we can include those packages in the program and then can use the respective classes in the Java program.

// keyword used to import all class from
// package java.io
import java.io.*;
public class FileManipulation
{
    public static void main(String rs[])
    {
         // File is a class from java.io package.
         // file has many inbuilt methods which can 
         // be used directly and one need not write
         // logic to handle those operations.
         File f = new File("path of a file");
    }
}
44 package
45 native
46 enum
47 void
48 instanceof

2 Keywords in Un Used category

Below are the 2 keywords in Java which are never used, but we should be aware if these keywords. As we know that we cannot declare any variable with keywords, so we need to know that we cannot create variables with below 2 keywords as well.

S.No Keyword
49 const
50 goto

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *