在Java编译器中,哪种类型可以定义为标识符(ID)或关键字(保留字)?
我有一个简单的问题:
在Java Compiler中,哪种类型的方法或变量可以定义为标识符(ID)或关键字(保留字)?
对于以下示例,ID应为: add
, main
, a
, b
, c
, Test1
,如何print
,是print
ID还是关键字?
例:
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"); } }
您cannot use
在java程序中cannot use
任何Keywords as identifiers
在上面的程序中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中的法律标识符
- java中的关键字和保留字列表
Java关键字是该语言的一部分,并在Java语言中进行了记录 。 您不能将关键字用作标识符。 const
和goto
是保留关键字,但未实现。 true
, false
和null
是文字; 您仍然不能将它们用作标识符,但它们不是关键字。
从链接的Java教程中,关键字是
- 抽象
- 继续
- 对于
- 新
- 开关
- 断言3
- 默认
- 转到1
- 包
- 同步
- 布尔
- 做
- 如果
- 私人的
- 这个
- 打破
- 双
- 器物
- 保护
- 扔
- 字节
- 其他
- import
- 上市
- 投
- 案件
- 枚举4
- 的instanceof
- 返回
- 短暂的
- 抓住
- 扩展
- INT
- 短
- 尝试
- 烧焦
- 最后
- 接口
- 静态的
- 空虚
- 类
- 最后
- 长
- strictfp 2
- 挥发物
- const 1
- 浮动
- 本地人
- 超
- 而
1未使用
2添加1.2
3增加了1.4
4在5.0中添加