Identifiers are well defined by The Java Language Specification, Java SE 7 Edition (§3.8)
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
As far as I know, since a method name is an identifier, It should be impossible to name a method starting with a digit in java, and javac
respects this rule.
So, why does the Java Virtual Machine seem to not respect this rule by allowing us to name a function starting with numbers, in Bytecode?
This simple snippet will actually print the f99()
method name and the value of its parameter.
public class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.f99(100));
}
public int f99(int i){
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
return i;
}
}
Compilation and execution:
$ javac Test.java
$ java Test
Output:
f99
100
It is possible to disassemble the code once compiled, and rename all f99
occurences by 99
(with the help of a tool like reJ).
$ java Test
Output:
99
100
So, is the name of the method actually "99"?