Keywords in Java

Java 8Object Oriented ProgrammingProgramming

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);
raja
Published on 07-Dec-2018 09:46:30
Advertisements