Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
109 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

  1. It's a pain to parse electronically.
  2. It's a pain for humans to parse.

Consider the following code snippet:

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

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

Also consider:

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...