syntax - What is the difference between keywords and reserved words in Java? - Stack Overflow
When are reserved words not keywords?
regex - Get a list of all Java reserved Keywords - Stack Overflow
Do you have reserved keywords that are currently not in use?
Videos
I read this link: https://stackoverflow.com/questions/1078908/what-is-the-difference-between-keyword-and-reserved-word#:~:text=Keywords%20have%20a%20special%20meaning,reserved%20words%20and%20vice%20versa.
I understand how reserved words are keywords and how some keywords cannot be reserved words, but I do not understand how some reserved words cannot be keywords? The answer to the post says how goto is not a keyword, but the java docs say how goto is a reserved word but also a keyword. Does this mean that reserved words are always keywords, or is there an instance/example in some language where a reserved word is not a keyword, would this mean that the StackOverflow post is wrong?
Certain language such as Python and C++ seem to use both of these terms interchangeably, so it depends on the language as well?
From axis.apache.org
Basically, Pre-Sort the keywords and store it in an array and using Arrays.binarySearch on your keyword for the good'ol O(logn) complexity
import java.util.Arrays;
public class MainDemo {
static final String keywords[] = { "abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements",
"import", "instanceof", "int", "interface", "long", "native",
"new", "null", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "true",
"try", "void", "volatile", "while" };
public static boolean isJavaKeyword(String keyword) {
return (Arrays.binarySearch(keywords, keyword) >= 0);
}
//Main method
public static void main(String[] args) {
System.out.println(isJavaKeyword("void"));
}
}
Output:
True
Alternatively, as users @typeracer,@holger suggested in the comments,
you can use SourceVersion.isKeyword("void") which uses javax.lang.model.SourceVersion library and Hashset Data structure internally and keeps the list updated for you.
I'm surprised that no one suggested javax.lang.model.SourceVersion yet, because it's actually been around since Java 1.6.
If you need to check whether some string is a reserved keyword, you can just call:
SourceVersion.isKeyword(str)
And if you really need the full list of the reserved keywords, you can obtain it from the source code of that class:
private final static Set<String> keywords;
static {
Set<String> s = new HashSet<String>();
String [] kws = {
"abstract", "continue", "for", "new", "switch",
"assert", "default", "if", "package", "synchronized",
"boolean", "do", "goto", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "strictfp", "volatile",
"const", "float", "native", "super", "while",
// literals
"null", "true", "false"
};
for(String kw : kws)
s.add(kw);
keywords = Collections.unmodifiableSet(s);
}
Caution: the above source code is from Java 1.8, so don't just copy & paste from this post if you're using a different version of Java. In fact, it's probably not a good idea to copy it at all โ they made the field private for good reason โ you probably don't want to have to keep it up-to-date for every new Java release. But if you absolutely must have it, then copy it from the source code in your own JDK distro, keeping in mind that you might have to manually keep updating it later.