"Hey, young recruit, who makes a bunch of mistakes every day, even in the simplest programs!
"Um... Hi, Diego. Boy, you really know how to cheer people up!"
"Of course, I do! Learn how to say what needs to be said. Like I said, 'every day'. Putting it more politely, my greeting means "future successful programmer, if, of course, you doesn't blow it."
"Oooh, well then thank you, my most metallic teacher!.
"You'll thank me after this lesson. Here I'm going to list the most popular errors noted by the compiler. And your job is to remember. Forewarned is forearmed.
Forgetting a semicolon
"The most common mistake aspiring Java programmers make involves the semicolon. Or rather, its absence where it should be."
"The truth is... I've committed this offense repeatedly."
"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 |
---|---|
|
|
|
|
|
|
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.
Incorrect code | Correct code |
---|---|
|
|
|
|
|
|
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 |
---|---|
|
|
|
|
|
|
Forgetting to close curly braces
"This is a very common mistake. There are two situations where this is typical:
- You decided to copy code from somewhere and accidentally missed some curly braces.
- You simply aren't troubling yourself to make sure that every open parenthesis is matched by a closing parenthesis."
"The second option is what I do. Sometimes I get so carried away that I forget!".
"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 |
---|---|
|
|
|
|
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 |
---|---|
|
|
|
|
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 |
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 |
---|---|---|
Solution.java | Solution | Everything is fine |
Solutions.java | Solution | The file name as a superfluous letter 's' |
solution.java | Solution | The file name starts with a lowercase letter |
Solution.txt | Solution | The file extension is .txt instead of .java |
Solution.java | solution | 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 |
---|
|
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."
"Ah-ha, so that's why each class should begin with an indication of the package that it belongs to."
"Exactly. Here's an example:
Code without package | Corrected example |
---|---|
|
|
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."
"Thank you, Diego. This lesson will make me pay more attention."
"That's what I was counting on. Good luck!"
GO TO FULL VERSION