Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't an identifier begin with a digit in java

Tags:

java

I can not think anything other than "string of digits would be a valid identifier as well as a valid number."

Is there any other explanation other than this one?

like image 298
Rıfat Erdem Sahin Avatar asked Nov 28 '22 01:11

Rıfat Erdem Sahin


2 Answers

Because that would make telling number literals from symbols names a serious PITA.

For example with a digit being valid for the first character a variables of the names 0xdeadbeef or 0xc00lcafe were valid. But that could be interpreted as a hexadecimal number as well. By limiting the first character of a symbol to be a non-digit, ambiguities of that kind are avoided.

like image 111
datenwolf Avatar answered Nov 30 '22 13:11

datenwolf


If it could then this assignment would be possible

int 33 = 44; // oh oh 

then how would the JVM distinguish between a numeric literal and a variable?

like image 32
Reimeus Avatar answered Nov 30 '22 15:11

Reimeus