Java Notes
Java keywords are words that Java already uses for its own instructions, such as class, public, if, and return. You cannot use them as names for your variables or classes.
A keyword is a word that already belongs to Java. Java uses it to understand your program. For example, class tells Java that a class is starting, and if tells Java to make a decision.
Because Java already has a job for these words, you cannot use them as names you create yourself. You can name a variable score, but not class.
See the problem first
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
int is a Java keyword that says “this variable stores a whole number.” score is a name chosen by the programmer. It is allowed because Java has no special job for the word score.
The line below is not allowed, so it is written as a comment. If you remove //, Java reports an error:
// int class = 10;Java sees class and expects a class declaration, not a variable name. Use a meaningful alternative such as classNumber, courseName, or sectionName.
You do not need to memorise every keyword
There are many Java keywords, but you learn them naturally while learning each topic. When you see a new keyword, ask one question: what job is Java giving this word?
Here are the groups you will meet first:
| When you want to... | Keywords you may see | Simple job |
|---|---|---|
| Start a class | class, public | Define a program class and say it can be used from outside. |
| Store values | int, double, boolean, char | Tell Java what kind of value a variable holds. |
| Make a decision | if, else, switch | Choose what code should run. |
| Repeat work | for, while, break, continue | Control loops. |
| Create an object | new | Make a new object from a class. |
| Finish or send back a result | return, void | End a method or describe what it gives back. |
You will learn the exact behaviour of each keyword in its own lesson. For now, recognise that these words have fixed meanings in Java.
Read keywords inside one program
This program checks whether a student's score reaches the pass mark. Read the explanation below it; each highlighted word has a job.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the important words like this:
public classstarts a class namedResultChecker.intsays thatPASS_MARKandscoreare whole numbers.staticletsmainusePASS_MARKwithout first creating a ResultChecker object.finalmeansPASS_MARKshould keep the value40and not be changed later.ifchecks a question: is score at least 40?elsegives Java the other action when the answer is no.
Do not try to remember the technical details of static and final today. The important point is that Java reads these keywords as instructions, while score and PASS_MARK are names created for this program.
Words that look special
true, false, and null are also words Java reserves for fixed values:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
true and false mean yes/no values. null means that a variable does not currently point to an object. You will learn null in more detail when working with objects and Strings.
Try it yourself
In the ResultChecker program, change score to 32, run it, and see the else part execute. Then try to rename score to a clear name such as studentScore. Do not rename it to if, class, or return; those names are reserved by Java.
Remember this
- Keywords are words Java already uses as instructions.
- You may use your own name such as
score, but not a keyword such asclass. - Learn keywords one topic at a time instead of memorising a long list.
Continue the lesson path