为什么标识符不能以数字开头? [英] Why can an identifier not start with a number?

查看:1162
本文介绍了为什么标识符不能以数字开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在java中(我不知道任何其他编程语言)标识符不能以数字开头,为什么以下声明也不允许?

Why in java (I dont know any other programming languages) can an identifier not start with a number and why are the following declarations also not allowed?

int :b;
int -d;  
int e#;
int .f;
int 7g;


推荐答案

一般来说,你出于两种原因设置了这种限制:

Generally you put that kind of limitation in for two reasons:


  1. 以电子方式解析是一件痛苦的事。

  2. 解析人类是一种痛苦。

请考虑以下代码段:

int d, -d;
d = 3;
-d = 2;
d = -d;

如果 -d 是合法标识符,那么 d 的最终价值是多少? -3还是2?这是不明确的。

If -d is a legal identifier, then which value does d have at the end? -3 or 2? It's ambiguous.

还要考虑:

int 2e10f, f;
2e10f = 20;
f = 2e10f;

最后 f 的价值是多少?这也是模棱两可的。

What value does f have at the end? This is also ambiguous.

此外,无论如何阅读都是一种痛苦。如果有人声明 2ex10 ,这是200万或变量名称的拼写错误吗?

Also, it's a pain to read either way. If someone declares 2ex10, is that a typo for two million or a variable name?

确保标识符开始with letters表示他们可以与之冲突的唯一语言项是保留关键字。

Making sure that identifiers start with letters means that the only language items they can conflict with are reserved keywords.

这篇关于为什么标识符不能以数字开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆