views:

204

answers:

8

Everything is fine when I declare

String a;

but it says Syntax error on token "1", invalid VariableDeclaratorId when I do this

String 1;

I guess it must be very trivial but I don't get it.

+3  A: 

The parser can't distinguish it from the int literal, so it's disallowed.

Ignacio Vazquez-Abrams
Why char and int literal treated so differently ?
Ravi Gupta
Char literals have quotes around them.
Ignacio Vazquez-Abrams
Because one of them is a name of a variable, the other one is a value. If you couldn't distinguish them by their naming conventions, how else should the compiler find out if it's dealing with a variable name or a value?
Kosi2801
+5  A: 

The rules for identifiers in the Java language specification state that you cannot start an identifier with a number.

Mark
+23  A: 

Well, first of all, it's because it's written in the Java language Specification.

But, maybe that this example will help you more:

String 1 = "toto"
System.out.println(1 + 2)

What should be the output?

gizmo
+7  A: 

Because 1 is also a value (which, among others, you can assign) the parser cannot know what you mean.

Consider the following snippet:

int 1 = 10;
int a = 1; // what is the value of a ? 1 or 10?

Therefore, starting a variable name with a number is dissallowed. You can use _1 instead if you really want (note that it is difficult to read though)

laura
Ok, but what is wrong in having var named as _1a_ ?
Ravi Gupta
Suffixes to variables make a distinction between different value types. For examples 1L means that this is a value of the type Long, not integer. Or 0x... means that there is a hex-value following.
Kosi2801
+2  A: 

Not only the parser would have a great deal of effort distinguishing between an int literal and a variable (if not totally impossible) but you could end up with strange situations like:

int 1 = 999;
System.out.println(1);

// output: 1 or 999

Basically this doesn't make much sense.

bruno conde
A: 

I think you can. It compiles on my machine.

walter
What planet is your machine on ...
Dan
What? Seriously?
Bobby
Yes, it does. here is the code: public static void main(String args[]) { String 1 = "one"; }it does compile on jdk1.1.3
walter
jdk1.1.3? Heh! Thats several decades ago! :-D
Andrew Dyster
Hi Dolphenstein, Not everybody can use the latest jdk. Huh ?
walter
nice to know that it was possible before Java 2 :)
IgorK
That's not true. This was never allowed in Java and also the 1.1.3 compiler would reject this. Altough you could always have a variable named "1" in the class file (still in the lastest Java 1.6), but not in the source.
x4u
A: 

Because 1 is a value. What will someone make of this :

String 1 = "6";
String s = 1 + "00";    // With value "100" or "600"?

Similarly, true, false, null cannot be variable names.

fastcodejava
A: 

It works on my machine too: public static void main(String args[]) { String l = "one"; } I am on jdk1.7

javaExpert
Yes, the lowercase letter "L" is a valid identifier in Java, as your compiler says. But the question wasn't about that. It was about the digit "1", which is not a valid identifier. Try it...
Paul Clapham