Java Reserved Words
In languages, some words are reserved to represent some meaning or functionality. In english for example, the word "cat" is reserved to represent a type animal, "dog" is a reserved word to represent a type of animal, "apple" is a reserved word to represent a type of fruit, "eat" is a reserved word to represent a type of action. In English, there a many such reserved keywords, take the dictionary for example it has an entire book of reserved English language keywords.
In the Java programming language there are 53 reserved words to represent some meaning or functionality. In the 53 reserved words, 50 of them are keywords and the other 3 are reserved literals. The difference between keywords and literals is, a keyword is a reserved word that is associated with functionality whereas a reserved literal is a reserved keyword that is only used to represent some value. In the 50 keywords, 48 are used keywords and the other 2 are unused keywords.
Java Reserved Words | |||||||||
---|---|---|---|---|---|---|---|---|---|
Keywords | Reserved Literals | ||||||||
Used Keywords | Unused Keywords | ||||||||
Enum keyword | Return type keyword | Object related keywords | Class related keywords | Exception handling | Modifiers | Flow control | Data types | ||
enum(v1.5) | void | new | class | try | public | if | boolean | const | false |
instanceof | interface | catch | private | else | byte | goto | null | ||
super | extends | finally | protected | switch | char | true | |||
this | implements | throw | static | case | double | ||||
package | throws | final | default | float | |||||
import | assert(v1.4) | abstract | while | int | |||||
synchronized | do | long | |||||||
native | for | short | |||||||
strictfp(v1.2) | break | ||||||||
transient | continue | ||||||||
volatile | return |
Although only 11 modifiers are shown in the table above, there are 12 modifiers in total. The 12th modifier is "default", which is the default modifier when no other modifiers are used.
In Java a method return type is mandatory, hence why the "void" keyword is there. So if a method does not return anything, it should be declared with "void" return type. But in C language, return type is optional, and the default return type is "int".
As for the unused keywords, the usage of goto created several problems in old languages and hence some people banned this keyword in Java. as for "const", use final instead of "const". "goto" and "const" are unused keywords, and if you try to use them, there will be a compile time error.
As for reserved literals, "true" and "false" are values for boolean data type. "null" reserved literals is the default value for object reference.
Enum is used when defining a group of named constants.
import java.lang.*;
enum Month {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER;
}
Conclusions
- All 53 reserved words in Java, only contain small-case alphabet characters.
- In Java, we only have the "new" keyword and there is no "delete" keyword in Java because destruction of useless objects is the responsibility of garbage collecter.
- The following are new keywords in Java, "strictfp" (v1.2), "assert" (v1.4), "enum" (v1.5).
- strictfp not strictFp
- instanceof not instanceOf
- synchronized not synchronize
- extends not extend
- implements not implement
- import not imports
- const not constant