我有一个简单的问题:
在Java编译器中,哪些类型的方法或变量可以被定义为标识符(ID)或关键字(保留字)?
对于以下示例,ID 应为:add
, main
, a
, b
, c
, Test1
, 关于什么print
, is print
ID 或关键字?
Example:
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");
}
}
An 标识符是程序员用来命名的一个词变量、方法、类或标签.
// 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");
}
}
You cannot use
任何一个Keywords as identifiers
在你的java程序中
print
在你上面的程序中不是Keyword
,您可以使用 print 作为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);
}
}
另请参阅:
- 检查java中的合法标识符@Peter Lawrey https://stackoverflow.com/questions/11774099/legal-identifiers-in-java/#answer-11774161
- java中的关键字和保留字列表 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)