Keywords in java – Part 2

               Keywords are important in any programming language. In java, we classified the keywords in many categories.

In part1 of blog post explains the first two categories of keywords. The link is given below.

https://rajeeva84.blogspot.com/2024/08/keywords-in-java-part-1.html

Here, remaining keywords and its usage are explained with examples.

3. Keywords based on objects and classes:

              Classes are the non-primitive datatypes. Each java program should start with a class. The class name is the file name where the main () function resides.

Object is a real-world entity. The keywords related to classes and objects are listed below.

abstract

extends

instanceof

return

synchronized

transient

this

void

static

public

private

protected

super

sealed

permits

native

new

null

 

Abstract: It deals with abstract classes and methods. This deals with defining a class or function for abstraction.

void: It represents that the function doesn’t return anything.

These are declared, but not defined. These class and function can be inherited by other class and define the functions.

Extends:

It deals with sub class. The child class extends the methods from parent class.

Eg: use the previous abstract class as parent.

new: it is used to create new object.

instanceof : It is used to check the object reference. If it is true, the object exists.

return: This returns the value.

synchronized: It deals with multithreading. When multiple threads share one resource, this block helps you to access. A sample block is given below.

synchronized(sync_object)

{

//statements to access variables and resources

}

transient: It is defined for variable, which is used in serialization.

this: it denotes the current object instance.

static: The value is retained across the program.

public: It is a access specifier. The member can be accessed by all( parent and child classes)

protected: This is used to access its parent and member functions.

private: This can be accessed by same class only.

super: It deals with base class.

sealed: It is introduced in java15.It is sealed. Unless otherwise it cannot be accessed without permits option.

native: It is defined by JNI(Java Native Interface). It is applicable for methods only.

null: it represents null value.

let us code an Example with some keywords:

abstract class abc

{

 abstract void display();

}

class  def extends abc {

void display()

{

System.out.println(“Welcome to the world of java”);

}

}

class  ghi

{

public static void main(String args[])

{

abc a = new def();

a.display();

System.out.println(object instanceof abc);

}

}


4. Keywords based on exceptions:

              Java is excellent in error handling. These are keywords used.

try

catch

final

finally

 

This is the syntax given below.

try and catch :

try

{

}catch(Exception e)

{

}

final and finally:

try

{

final a;

}

finally()

{

}

5. Others:

    These are the other keywords listed below.

assert

enum

interface

package

assert:It is a statement executed when assert function is called.

Eg:

Assert a > b : “a is the biggest number”;

enum: It represents a collection of named objects.

Eg:

Enum a {

1,2,3;}

interface : It is like a blueprint of class and functions behavior.

Eg:

Interface f1

{

final int s;

void a();

}

package : It is a collection of classes and objects.

Eg: package a; //user defined package.

Import java.io.*; //built in package.

 These are the keywords used in java.

No comments:

Post a Comment