1. List of words

As in any programming language, Java has words that have special meaning. For example, return or if or while. These words are called keywords (keywords) and are considered reserved by the Java language.

You cannot use these words as a variable name, method name, or class name. The compiler will always interpret them in a strictly defined way. There are 54 such words in Java.

abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
var
true
null
false

You already know some of them, and we'll talk about the rest right now.


2. Primitive types

As you probably remember, Java has 8 primitive types, and each of them has its own keyword:

  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean
  • void

If you have sufficiently inquisitive mind, there's a good chance you already tried to name a variable int. And of course you didn't succeed. This is precisely because the names of all of the primitive types are reserved words.

The void type also falls into this category.


3. Loops and branches

Loops and branches also give us a rather long list of keywords:

  • if
  • else
  • switch
  • case
  • default
  • while
  • do
  • for
  • break
  • continue

Just 10 words is enough for the language to provide several kinds of loops, branches, and control statements for interrupting loops (break and continue) and multiple branches (switch). You are already familiar with all of these keywords.


4. Exceptions

Exceptions give us 5 keywords:

  • try
  • catch
  • finally
  • throw
  • throws

These are all part of a try-catch-finally block. The operator for throwing exceptions is throw, and the throws keyword supports the checked exception mechanism.

The good news is that you are already familiar with all the keywords related to exceptions, so you are also already mostly familiar with working with exceptions.


5. Visibility

Here there are only three keywords, and you are already familiar with them.

  • private
  • protected
  • public

public allows access to a method/variable/class from anywhere in the program.

private prohibits a method/variable/class from being accessed from just anywhere in the program. Access is only allowed within the same class as a method marked with the private modifier.

protected works the same as private, but also allows access to a method/variable/class from inherited classes. The benefits of this modifier will become clearer to you when you become familiar with OOP and inheritance.


6. Working with classes

There are 11 keywords in this category:

  • class
  • interface
  • enum
  • import
  • package
  • extends
  • implements
  • static
  • final
  • abstract
  • default

They can be divided into 4 groups.

The first group relates to the creation of classes: class, interface and enum. You already learned about declaring classes and enums. The interface keyword is used to declare yet another class-like type: interfaces.

The second group consists of the package and import keywords, which you are already familiar with. The package keyword is used to specify the package of a class in a class file. And import is so we can use short names of external classes when writing our own classes.

The extends and implements keywords are used for inheritance. You will take a look at them at the very beginning of the Java Core quest.

Finally, the last group consists of the static, final, default, and abstract modifiers. You already know a little about static and final. The abstract keyword is used to make a class or method abstract. You'll get more details when studying inheritance in the Java Core quest.


7. Working with objects and variables

Six more keywords are used when working with objects, methods, and variables.

  • new
  • instanceof
  • this
  • super
  • return
  • var (since Java 10)

The new operator is used to create new objects — you already know that.

The instanceof operator is used to verify that a variable contains a reference to an object of a particular type. You are already familiar with it.

The this keyword is used to solve problems that arise due to the shadowing of instance variables and methods. You've studied this as well.

The super keyword is analogous to this, but it is used to refer to the methods and variables of the parent class. The parent class is also called the superclass.

The return statement is used to return the value of a method, and also to terminate the execution of a method.

Finally, var is for declaring a variable whose type is automatically inferred. You are already familiar with this.


8. Multithreading

At the level of Java syntax, multithreading is represented by just two words.

  • synchronized
  • volatile

We won't even touch them. Get to the Java Multithreading quest, and then we'll dive in.


9. Miscellaneous

There are another 4 special keywords:

  • native
  • transient
  • assert
  • strictfp

native is a modifier that can be written before a method declaration. It means that the method code is written not in Java, but in C++ and is embedded in the Java machine (well, or a DLL). As you probably already guessed, the Java machine itself is also written in C++. Just like many of the standard library methods.

transient is a modifier that can be written before instance variables (fields of a class). It asks the Java machine to skip (or ignore) the marked variable while serializing an object of the class. You can learn more about serialization in the Java Collections quest.

assert also comes to Java from C++. With its help, you can add additional checks to your code (for example, to check whether a variable is null). The important thing here is that these checks are enabled or disabled at compile time.

You can build the project for internal testing, and these checks will be carried out (included in the build). Or you can disable them during compilation to create the version of the program that will be supplied to users.

As for the strictfp keyword and the extra precision of Intel processors, we have a whole story for you.


10. Reserved but not used

There are also two keywords that are reserved but not used.

  • const
  • goto

These are also a legacy of the C++ language, where they exist and are used.


11. Not keywords

Formally, the true, false and null constants are not keywords. That said, they each have their peculiarities. You cannot name a method true or a variable false. The compiler will not understand such code and will not compile it.