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
int a
int b = 5
int c = a + b
int a;
int b = 5;
int c = a + b;
System.out.println("Hello")
System.out.println("Hello");
if (2 > 3)
   System.out.println("Are we in Australia?")
if (2 > 3)
   System.out.println("Are we in Australia?");


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
String s = "Hello;
String s = "Hello";
System.out.println("Hello);
System.out.println("Hello");
String s = "Hello";
String message = s + " and by. ;
String s = "Hello";
String message = s + " and bye.";


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
String s = "Hello";
String message = s  " and bye.";
String s = "Hello";
String message = s + " and bye.";
int age = 35;
System.out.println("Age=" age);
int age = 35;
System.out.println("Age=" + age);
int age = 35;
System.out.println("Age=", age);
int age = 35;
System.out.println("Age=" + age);


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
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
}
{
   if (2 < 3)
   {
      if (3 < 4)
      {
         System.out.println("Mathematics!");
      }
   }
{
   if (2 < 3)
   {
      if (3 < 4)
      {
         System.out.println("Mathematics!");
      }
   }
}


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
System.out.println("Hello!");
System.out.println;
System.out.println("And bye!");
System.out.println("Hello!");
System.out.println();
System.out.println("And bye!");
if 2 < 3
{
   if 3 < 4
   {
      System.out.println("Mathematics!");
   }
}
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
}


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
static void main(String[] args)
public is missing
public void main(String[] args)
static is missing
public main(String[] args)
void is missing
void main(String[] args)
public and static are missing
public static void main(String args)
[] is missing
public static void main()
String[] args is missing
public static int main(String[] args)
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
Solution.java
Solution
Everything is fine
Solutions.java
Solution
The file name has 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
public class Solution
{
}

class Apple
{
}

class Pineapple
{
}

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
public class Solution
{
   public class Apple
   {
   }

   public static class Pineapple
   {
   }
}


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
public class Solution
{
}
package com.codegym.tasks.task0001;

public class Solution
{
}


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
public class Solution
{
   java.util.Scanner scanner = new java.util.Scanner();
}
import java.util.Scanner;

public class Solution
{
   Scanner console = new Scanner();
}

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.