Java关键字

Java中的关键字是保留字,代表预定义的操作,内部过程等。因此,关键字不能用作变量,函数,对象等的名称。

关键字和标识符之间的主要区别在于,关键字是保留字,代表预定义的动作,而标识符是变量,函数,对象等的名称。

Java中的一些关键字如下所示-

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

演示关键字的程序如下:

示例

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);
}
}

输出结果

i = 5
c = A

现在让我们了解上面的程序。

上面程序中的关键字是int和char,分别指定整数和字符数据类型。i和c也是标识符。

在上面的程序中,定义了i和c的值,然后将它们打印出来。演示此过程的代码段如下所示。

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