我有一个简单的问题: 在Java编译器中,可以将哪种类型的方法或变量定义为标识符(ID)或关键字(保留字)?
对于下面的例子中,ID应该是:add,main,a,b,c,Test1,怎么样print,是print一个ID或关键字?
add
main
a
b
c
Test1
print
例:
public class Test1 { public static int add(int a, int b) { return a + b; } public static void main() { int c; int a = 5; c = add(a, 10); if (c > 10) print("c = " + -c); else print(c); print("Hello World"); } }
一个 标识符 是用于由程序员,仅举一个字 变量,方法,类,或标签 。
// Test1 is a class name identifier public class Test1 { public static int add(int a, int b) { // add is identifier for a method return a + b; } public static void main() { int c; // c is identifier for a variable int a = 5; c = add(a, 10); if (c > 10) print("c = " + -c); else print(c); print("Hello World"); } }
您在Java程序中的cannot use任何一个Keywords as identifiers
cannot use
Keywords as identifiers
print在您上面的程序中不是Keyword,您可以将print用作identifier
Keyword
identifier
使用print作为标识符后,您的代码如下所示。
//Test1 is a class name identifier public class Test1 { // add is identifier for a method public static int add(int a, int b) { return a + b; } public static void main(String[] args) { int c; // c is identifier for a variable int a = 5; c = add(a, 10); if (c > 10) print("c = " + -c); // c is a String else print(c); // c is a int print("Hello World"); // Hello World is a String } /** * Method Overriding */ private static void print(int c) { System.out.println("In Integer Print Method "+c); } private static void print(String string) { System.out.println("In String Print Method "+string); } }