"Hi, Amigo! Let's continue to talk about mistakes. This time, we'll explore mistakes that the compiler will not always help you with. Pay attention and you'll learn something about yourself."

"I'm ready to listen, Diego. I hope this won't be too embarrassing for me."

Comparing objects with ==

"Our list of top favorite newbie programmer mistakes begins with comparing objects (especially strings) using the == operator"

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");
 }

"I've done that quite often. Now I can clearly see that this code will never display "The strings are equal", because the if statement compares references to two different string objects."

"Yes. That's why the correct option would be:

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");
 }

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."

"It wasn't that long ago that I learned what immutable means, but I think I've done this.

"I'm fairly certain of it. Example:

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

"This code is very similar to 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

"Exactly. I've done that, but I didn't even really understand what was wrong. Thank you for the clarification!"

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. Correct version:

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

Using a local variable instead of an instance variable.

"Newbies don't like to come up with long and meaningful names for variables."

"That's so true. For get things done quickly, I sometimes give variables names like a, b, and i."

"Don't do that. 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;
     }
   }
 }

"It's much more difficult to make mistakes in code with proper names. 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;
       }
    }
 }

Removing a collection item

"Have you already looked into collections?"

"Literally with just one eye."

"If you don't know what I'm talking about, then make a note to yourself to take a look in the future. 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 );

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

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();
   }
 }

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;
    }
 }
The constructor name is invalid. It 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.

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, or, more precisely, implements it, 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 Meov
 {
 }

Omitting break in a switch statement

"And the last mistake for 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;
 }

"You know, Diego... Judging by the set of errors you presented here, it feels like you've been reading my personal journal... Or you've been watching me solve tasks."

"Ha! Have no doubt about it. I have read, tracked, and continue to do so. So be on the alert!"

"???"

"Don't you worry. I'm just kidding. Be vigilant and make fewer stupid mistakes."