Where All the Keywords are stored in java? Is there any package to store the set of keywords(predefined thing) Because these are not related to any "Classes(capital C), but "class"(small c) itself is a keyword, and all primitive types,all specifiers, interface are keywords. but where they are stored? Definitely not in any Class(they must have defined in some package or somewhere) but where??
It is doubtful that all keywords are stored somewhere in the java <em>runtime</em> library, as they are not needed there.
Where they are needed, however, is in the compiler.
After giving the JDK langtools repo a quick look, I found that:<ul><li>
com.sun.tools.javac.antlr.Java.g4 (A text file)
Contains a list of keywords:
// §3.9 Keywords ABSTRACT : 'abstract'; ASSERT : 'assert'; BOOLEAN : 'boolean'; BREAK : 'break'; BYTE : 'byte'; CASE : 'case'; CATCH : 'catch'; CHAR : 'char'; CLASS : 'class'; CONST : 'const'; CONTINUE : 'continue'; DEFAULT : 'default'; DO : 'do'; DOUBLE : 'double'; ELSE : 'else'; ENUM : 'enum'; EXTENDS : 'extends'; FINAL : 'final'; FINALLY : 'finally'; FLOAT : 'float'; FOR : 'for'; IF : 'if'; GOTO : 'goto'; IMPLEMENTS : 'implements'; IMPORT : 'import'; INSTANCEOF : 'instanceof'; INT : 'int'; INTERFACE : 'interface'; LONG : 'long'; NATIVE : 'native'; NEW : 'new'; PACKAGE : 'package'; PRIVATE : 'private'; PROTECTED : 'protected'; PUBLIC : 'public'; RETURN : 'return'; SHORT : 'short'; STATIC : 'static'; STRICTFP : 'strictfp'; SUPER : 'super'; SWITCH : 'switch'; SYNCHRONIZED : 'synchronized'; THIS : 'this'; THROW : 'throw'; THROWS : 'throws'; TRANSIENT : 'transient'; TRY : 'try'; VOID : 'void'; VOLATILE : 'volatile'; WHILE : 'while';
I'm unsure whether that file is also included in the distributed javac, and thus could be retrieved at run time.</li> <li>com.sun.tools.javac.parser.Tokens (A normal java class)
Contains a subclass enum
TokenKind, which holds all tokens.
While these could definitely be retrieved at run time, they also include operators, an end-of-file representation, etc., so you'd have to filter them first.</li> </ul>
In theory, there is no need for the Java runtime environment, where your compiled code runs, to keep the keywords. Once compiled, all the code becomes bytecode. A private method, for example, would be marked by a flag rather than the string of letters p-r-i-v-a-t-e. A for loop would be represented by some byte code that sets the initial value of the variable, performs the operation, increments the value, checks the condition, and jumps back to the beginning of the operation. No need for the three letters f-o-r to be stored anywhere for this.
However, standard Java does have a class in the runtime library which contains all of the Java keywords - the SourceVersion enum in the javax.lang.model package. This enum has a method which accepts a
CharSequence and checks whether it is a keyword in the latest release of Java (the running version, obviously).
javax.lang.model package is offered for purposes of modeling the Java language. For example, it's being used by programs that process annotations or by programs that themselves compile Java programs.
SourceVersion enum <em>stores</em> all the keywords, but it doesn't make them available to you. The API merely allows you to check if a string is a keyword or not.