1. Forgetting a semicolon
The most common mistake aspiring Java programmers make involves the semicolon. Or rather, its absence where it should be.
Every statement inside a method must end with a semicolon. The semicolon is what separates statements or commands: this is how we tell the Java compiler where one command ends and the next begins.
Examples of errors:
Incorrect code | Correct code |
---|---|
|
|
|
|
|
|
2. Forgetting to close quotes
The second most common mistake for newcomers to Java is writing a string in the code and then forgetting to close the quote.
Every string literal in the code must be enclosed on both sides with double quotation marks ("). Beginner programmers very often put quotation marks at the beginning of text, but they forget to close them at the end.
Here are some examples:
Incorrect code | Correct code |
---|---|
|
|
|
|
|
|
3. Forgetting to include a plus sign when gluing together strings
Another common mistake when working with strings is forgetting to write a plus sign when gluing strings together. This error is especially prevalent when text and variables are concatenated in a long expression in code.
Here are some examples:
Incorrect code | Correct code |
---|---|
|
|
|
|
|
|
4. Forgetting to close curly braces
This is a very common mistake. There are two situations where this is typical:
First situation: You decided to copy code from somewhere and accidentally missed some curly braces. Second situation: You simply aren't troubling yourself to make sure that every open parenthesis is matched by a closing parenthesis.
To avoid these errors, it is usually recommended for beginner programmers to write the closing curly brace underneath the opening one.
Examples:
Incorrect code | Correct code |
---|---|
|
|
|
|
5. Forgetting to add parentheses
Most often this mistake is made by developers who know programming languages that don't require parentheses in similar situations.
One possibility is that they simply forget to put parentheses at the end of a method call:
Another possibility is that they forget to wrap the condition of an if-else
statement in parentheses.
Examples:
Incorrect code | Correct code |
---|---|
|
|
|
|
6. Writing the main
method declaration incorrectly
As soon as they declare the bloody main
method! There is probably nothing that trips up newbies as much as this poor method. Importantly, then they are always surprised and wonder why their program won't start?
And, of course, the programmer isn't to blame, but the program, the compiler, the code validator, the Java machine, etc. The list of scapegoats is endless.
Examples:
Incorrect code | Explanation |
---|---|
|
public is missing |
|
static is missing |
|
void is missing |
|
public and static are missing |
|
[] is missing |
|
String[] args is missing |
|
We have int instead of void |
7. The file name is different from the class name
According to the Java standard, all Java classes must be stored in a file with the same name as the class name. And as mentioned earlier, the case of the letters matters here:
File name | Class name | Note |
---|---|---|
|
|
Everything is fine
|
|
|
The file name has a superfluous letter s |
|
|
The file name starts with a lowercase letter |
|
|
The file extension is .txt instead of .java |
|
|
The class name starts with a lowercase letter
|
Actually, several classes can be declared in a file with the .java extension, but only one of them can have the word public
before the class name. And this is the name that must match the file name.
A .java file must always have a class whose name is the same as the file name, and that class needs to have the public
modifier. Example:
Solution.java |
---|
|
Additionally, the Java language lets you write classes within classes. This is another way to get around the above limitation. If a public class (a class with the public
modifier) is declared in a file and has the same name as the file name, then you can declare as many classes as you like inside this public class. That said, these will no longer be ordinary classes. Instead, they will be internal or nested classes. Example:
Solution.java |
---|
|
8. Forgetting to write package
Since programs usually have thousands of classes, it can be difficult to find simple, understandable, and unique names for all of them. That's why in Java it is customary to group classes into packages using the package
keyword. Exactly the way the files are grouped into folders.
That's why each class should begin with an indication of the package that it belongs to. Example
Code without package | Corrected example |
---|---|
|
|
9. Forgetting to add import
If we want to use somebody else's class in our program, we have two options: everywhere in our code we must also write its package name before the name of the class. Alternatively, we can write the fully qualified class name with the import
keyword once.
Example:
Without using import | Using import |
---|---|
|
|
Both options work, but if you simply write Scanner
in your code without adding import
, then the compiler will not be able to understand which package it needs to take the Scanner
class from, and your program won't compile.
GO TO FULL VERSION