问题描述
我有一个简单的问题:
在Java编译器中,可以将哪种类型的方法或变量定义为标识符(ID)或关键字(保留字)?
I have a simple question:
In Java Compiler, Which type of method or variables can be defined as identifier (ID) or Keyword (reserved word)?
对于下面的示例,ID应该是:add
,main
,a
,b
,c
,Test1
,print
怎么样?print
是ID还是关键字?
For following example, ID should be : add
, main
, a
, b
, c
, Test1
, What about print
, is print
an ID or keyword?
示例:
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 identifier is a word used by a programmer to name a variable, method, class, or label.
// 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");
}
}
您cannot use
您的Java程序中的任何Keywords as identifiers
You cannot use
any of the Keywords as identifiers
in your java programs
print
不是Keyword
,您可以将print用作identifier
print
in your above program is not a Keyword
, you can use print as a identifier
使用print作为标识符后,您的代码如下所示.
After using print as a identifier your code looks like this.
//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);
}
}
另请参阅:
- Check Legal Identifiers in java @Peter Lawrey
- List of Keywords and Reserved Words in java
这篇关于在Java编译器中,可以将哪种类型定义为标识符(ID)或关键字(保留字)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!