There are some misunderstandings and confusion regarding keywords and reserved words in Java. I’ve seen many articles treating reserved words as keywords but actually, there is a difference.
Java has many keywords, let’s list them here:
abstract | continue | for | new | switch |
assert | default | super | package | synchronized |
boolean | do | if | 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 |
float | native | while |
If you want to further explore the keywords, you can check the official Oracle keywords documentation.
There are some special restrictions in the Java keywords. The code won’t compile if we create a class, method, or variable with any Java keyword as the identifier.
If we try for example to create a variable with the following name, the code won’t compile:
int break = 5;
We will have the “Unexpected Token” compilation error message.
If we try to create a method name with a keyword we will also have an “Unexpected Token” compilation error:
class NativeMethod { public void continue() { } }
Reserved words
There are some words in Java that were reserved for “future use”, they are unimplemented keywords. However, chances are that those reserved words will never be used:
const – was reserved by the Java architects for a possible reason for avoiding confusion with C++ developers. The final keyword is used instead.
goto – was reserved and implemented by James Gosling but it’s better to use more readable names such as continue and break. You can watch his video saying it himself: James Gosling on Apple, Apache, Google, Oracle and the Future of Java.
-
Reserved words for Literals
- true – A boolean literal value.
- false – A boolean literal value.
- null – A reference literal value. If you want to further explore what means each of the keywords and reserved words in Java, you can check the following Wikipedia link, List of Java keywords.
-
Reserved Identifiers
In the most recent Java releases, reserved identifiers that can be used as a Java identifier. To make a comparison between the most recent reserved identifiers and the previous keywords or reserved words; reserved identifiers can be used as a class, method, or variable names and keywords can’t!
The Java architects value retro-compatibility. That’s why they’ve made a decision to create reserved identifiers that can be used as class/method/variables names. Otherwise, when upgrading the Java version, developers could have compilation errors.
Let’s explore some of the reserved identifiers:
- sealed – A sealed class or interface can only be extended or implemented only by classes and interfaces permitted to do so.
permits – The permits clause specifies the classes that are permitted to extend a sealed class.record- A record is a special Java class that creates constructors, getters, setters, equals, and hashcode avoiding boilerplate code.var – A special identifier that allows using type inference, this means that the compiler will figure out the variable typeyield – Returns a value within a switch case expression.
Therefore, what do you think will happen in the following code? Suppose you are using Java 15.
public class TheReservedChallenge { public static void main(String[] args) { var permits = "Permitting"; var record = 777; var sealed = Double.valueOf(0.0); var var = "ThisIsAVar"; var yield = 8_000; System.out.println(permits + record + sealed + var + yield); } }
You were right if you guessed that the output will be the following:
Permitting7770.0ThisIsAVar8000
That’s because when we use these reserved identifiers as variable names the code will compile without errors.
On the other hand, if we use previous Java versions keywords. What do you think will happen in the following code?
var const = "Challenger"; var break = 777; var import = Double.valueOf(0.0);
Since we are using keywords, there will be a compilation error with the following message: “Unexpected token”.
What will happen when running the following code?
class WrapperVariableNames { public static void main(String... doYourBest) { String String = "Game of Thrones"; Double Double = 9.D; Float Float = 9.f; System.out.println(String + Double + Float); } }
If you guessed that the code above it will compile fine and will print “Game of Thrones9.09.0” you were right! Congratulations!
Conclusion
In simple words, reserved identifiers can be used as Java identifiers but previous Java version keywords can’t. We shouldn’t use reserved identifiers as variables or method names though because this would make the code confusing. Using the wrapper class name as a variable name is another thing we should never do.
It’s very important to know what is possible to do with Java but we should also create readable code. Therefore if we use ambiguous variable or method names, we will create a hard to maintain code. Also, remember that by knowing the Java language very well, you will be able to fix bugs by the root cause with a masterful code.
As always, stay constantly breaking your limits! Leave a comment if you have any feedback or question!
Good and simple article about reserved words in Java!
Hi Miguel, thanks a lot for your comment! Stay tuned for more articles and Java Challenges!