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

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - Why can an identifier not start with a number?

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...