Keywords in Java


Keywords in Java are reserved words that represent predefined actions, internal processes etc. Because of this, keywords cannot be used as names of variables, functions, objects etc.

The main difference between keywords and identifiers is that keywords are reserved words that represent predefined actions while identifiers are the names of variables, functions, objects etc.

Some of the keywords in the Java are given as follows −

abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile

A program that demonstrates keywords is given as follows −

Example

 Live Demo

public class Example {
public static void main(String[] args) {
int i = 5;
char c = 'A';
System.out.println("i = " + i);
System.out.println("c = " + c);
}
}

Output

i = 5
c = A

Now let us understand the above program.

The keywords in the above program are int and char that specify integer and character data types respectively. Also i and c are identifiers.

In the above program, the values of i and c are defined and then they are printed. The code snippet that demonstrates this is given as follows.

int i = 5;
char c = 'A';
System.out.println("i = " + i);
System.out.println("c = " + c);

Updated on: 26-Jun-2020

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements