1. Comparing objects with ==

The favorite mistake of newbie programmers is comparing objects (especially strings) using the == operator. For example:

Scanner console = new Scanner(System.in);
String s1 = console.nextLine();
String s2 = console.nextLine();
if (s1 == s2)
{
   System.out.println("The strings are equal");
}

This code will never display "The strings are equal", because the if statement compares references to two different string objects.

Here is the correct version of the code:

Scanner console = new Scanner(System.in);
String s1 = console.nextLine();
String s2 = console.nextLine();
if (s1.equals(s2))
{
   System.out.println("The strings are equal");
}


2. Changing a String object

Newbie programmers often forget that all objects of the class are immutable, and that every method of the String class returns a new object — the current object never changes.

Example:

String s = "Hello";
s.toUpperCase (); // Convert to uppercase

This code is very similar to the correct code, but it won't work as expected. The toUpperCase() method does not change the object on which it is called. The correct code would look like this:

String s = "Hello";
String result = s.toUpperCase(); // Convert to uppercase


3. Forgetting to initialize objects that are elements of an array

Another common mistake is to forget to initialize an array variable. Example:

int[] array;
array[0] = 1;
array[0] = 2;

This code won't work: you need to explicitly set the array variable equal to a reference to the container object that will store the elements of the array.

int[] array = new int[10];
array[0] = 1;
array[0] = 2;


4. Using a local variable instead of an instance variable.

Newbies don't like to come up with long and meaningful names for variables. They frequently use single-letter names: a, b, i, etc. That's a cruel thing to do when the code has several variables like that:

Put the number 99 into 100 cells of an array
class Solution
{
  public static int a = 99;
  public static int i = 100;

  public static void main(String[] args)
  {
    int[] a = new int[i];
    for (int i = 0; i < 10; i++)
    {
      a[i] = a;
    }
  }
}

The code above will not compile. The correct version looks like this:

Put the number 99 into 100 cells of an array
class Solution
{
   public static int value = 99;
   public static int count = 100;

   public static void main(String[] args)
   {
      int[] a = new int[count];
      for (int i = 0; i < count; i++)
      {
         a[i] = value;
      }
   }
}


5. Removing a collection item

Very often there are situations when a certain element needs to be removed from a collection. The code looks roughly like this:

ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 0, -5, -7, -12, 5, 15);

for (Integer value: list)
   if (value < 0)
      list.remove(value);

This code will not work, because you cannot use a for-each loop to simultaneously traverse the elements of a collection and modify that collection.

There are several solutions. First, you can go traverse one collection and change another:

Solution 1
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 0, -5, -7, -12, 5, 15);

ArrayList<Integer> copy = new ArrayList<Integer>(list);
for (Integer value: copy)
   if (value < 0)
      list.remove(value);

Second, since Java 8, collections have a removeIf() method, to which you can pass a rule (lambda function) that indicates which elements to remove.

Example:

Solution 2
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 0, -5, -7, -12, 5, 15);

list.removeIf( x-> x<0 );


6. Placing several classes with the public modifier into a single file

There can be only one public class in a file. More classes can be declared in a file, but they must either be inner classes of a public class, or not have the public modifier. Example:

Contents of the Solution.java file Note
public class Solution
{
}
public class Main
{
}
This is not allowed: two public classes in a single file.
public class Solution
{
}
class Main
{
}
But you can do this. The Main class is not public
public class Solution
{
  public static class Main
  {
  }
}
And you can do this. The Main class is a nested class


7. Calling ordinary (non-static) methods of a class from the static main() method

Sometimes newbie programmers try to access non-static variables and methods from the main() method or other static methods. Such code will not work, of course.

public class Solution
{
   public int n = 100;
   public int[] createArray()
   {
      return new int[n];
   }

   public static void main(String[]args)
   {
      int[] array = createArray();
   }
}

The main method can only refer to static methods/variables. Well, or it must first create an instance of the Solution class, and only then call non-static methods of that object. Example:

Solution 1 Solution 2
public class Solution
{
  public static int n = 100;

  public static int[] createArray()
  {
    return new int[n];
  }

  public static void main(String[]args)
  {
    int[] array = createArray();
  }
}
public class Solution
{
  public int n = 100;

  public int[] createArray()
  {
    return new int[n];
  }

  public static void main(String[]args)
  {
    Solution sol = new Solution();
    int[] array = sol.createArray();
  }
}


8. Declaring a constructor like a method

Another common mistake is incorrectly declaring a class constructor. The name of a constructor must be the same as the name of the class, and a constructor has no result type. The most common mistakes look like this:

public class Person
{
   private String value;

   void Person(String value)
   {
      this.value = value;
   }
}




There shouldn't be a return type here
public class Person
{
   private String value;

   constructor(String value)
   {
      this.value = value;
   }
}




Invalid constructor name. The name of the constructor must match the class name
public class Person
{
   private String value;

   Person(String value)
   {
      value = value;
   }
}






this is missing. The value variable will be assigned to itself
public class Person
{
   private String value;

   Person(String value)
   {
      this.value = value;
   }
}




That is all correct


9. Incorrect inheritance of interfaces

Java's creators tried to make it very close to English, so they chose different keywords for certain related concepts.

When a class inherits a class, you have to use the extends keyword:

class Pet
{
}

class Cat extends Pet
{
}

When a class inherits an interface, you need to use the implements keyword:

interface Meow
{
}

class Cat implements Meow
{
}

When an interface inherits an interface, use the extends keyword:

interface Meow
{
}

interface Voice extends Meow
{
}


10. Omitting break in a switch statement

And the last mistake for us today, but not the last one for beginners, is failing to include a break statement in a switch statement. Example

Wrong Right
LocalDate date = LocalDate.now();
DayOfWeek day = date.getDayOfWeek();
switch (day)
{
   case MONDAY:
      System.out.println("Monday");
   case TUESDAY:
      System.out.println("Tuesday");
   case WEDNESDAY:
      System.out.println("Wednesday");
   case THURSDAY:
      System.out.println("Thursday");
   case FRIDAY:
      System.out.println("Friday");
   case SATURDAY:
      System.out.println("Saturday");
   case SUNDAY:
      System.out.println("Sunday");
}
LocalDate date = LocalDate.now();
DayOfWeek day = date.getDayOfWeek();
switch (day)
{
   case MONDAY:
      System.out.println("Monday");
      break;
   case TUESDAY:
      System.out.println("Tuesday");
      break;
   case WEDNESDAY:
      System.out.println("Wednesday");
      break;
   case THURSDAY:
      System.out.println("Thursday");
      break;
   case FRIDAY:
      System.out.println("Friday");
      break;
   case SATURDAY:
      System.out.println("Saturday");
      break;
   case SUNDAY:
      System.out.println("Sunday");
      break;
}