Last Updated:

A selection of popular mistakes of novice Java programmers

In this article, we will look at a selection or even a rating of 10 errors that are most often made by novice Java developers. The collection includes errors related to the use of data types in Java, utility methods for processing arrays and collections, the use of OOP in Java, strings and other important topics.

Incorrect array conversion to ArrayList

Most often it looks like this:

Then we work with this list: attempts to add/ remove elements, updating existing ones, etc.

In this situation, remember that the list that the Arrays.asList() method returns is an internal static class within the class (class) and, as you can see, it is not an instance of the class. An instance of a class has methods, , methods, and no ability to add elements because its size is fixed.

Arraysjava.util.Arrays.ArrayListjava.util.ArrayListjava.util.Arrays.ArrayListset()get()contains()

The correct way out in this situation is to create an instance of the class. Here's how to do it:

java.util.ArrayList

Separately, I want to note that this is one of the most popular mistakes among novice programmers. It appears due to the fact that a person began to study the SDK, superficially got acquainted with the methods for processing arrays, found the method of interest to him, but did not delve into the details of how it works and whether it does at all what is expected of it. This approach is most common among beginners who process a lot of new Information on Java into short lines. But for the sake of justice, it is worth noting that more experienced developers often sin with this.

Read more about working with arrays here. And about the Arrays.asList() method itself here.

Complex checking whether the array contains a value

Another interesting mistake that beginners constantly make. Let's look at an example of a popular verification option:

As you can see, we have a method to check if the array contains an element. Its essence boils down to the fact that we wrap the input array in the HashSet and use the standard collection methods to find the element. It's convenient because everyone did 🙂 it this way This method will work, but it will work longer than other methods, and write it longer.

We don't need to convert a list to a set just to use a convenient method. And the conversion itself will take additional time.

The best way out in this situation, I would call the use of this method:

As you can see, the approach is similar, but is written in only 1 line. Or remember the easiest and most reliable way:

If I were to choose from these two methods of checking whether the array contains a value, then I would choose the first one - for convenience and conciseness. And what do you use in such a situation? Write in the comments.

Trying to delete an item while crawling a list

Consider a code example that attempts to delete an item while crawling list items:

This code will be thrown away during execution because you cannot delete items while iterating through the list. The same situation will be with the for-each cycle. You can only safely remove items when using the Iterator interface, for example:ConcurrentModificationException

Always use an ArrayList and don't know where to use LinkedList

When the developer does not know the difference between and , then most often he gives preference - this is simple and familiar. However, there is a huge difference in performance between these collections. In short, it is advisable to use if there is a large number of operations to add or remove. For more information about this collection and others, see the Java Collections overview article.

ArrayListLinkedListArrayListLinkedList

Don't know the difference between the mutability or immutability of objects in Java

Immutable objects have a number of advantages over changeable ones, such as simplicity and security. However, there are drawbacks: they require a separate object for each new value, and many objects can lead to a high cost of garbage collection. Therefore, you should always keep a balance when choosing between mutable and immutable objects.

In short, modifiable objects are used to avoid a large number of intermediate objects. A great example would be concatenation of a large number of lines: if you use a string, then many objects are created along the way that should immediately go to garbage collection. This wastes CPU time, so the right solution with StringBuilder.

Read more about this topic in the articles: TOP-10 questions by lines, Why are the lines unchanged?.

Don't know the difference between creating strings using the constructor and double quotation marks ("")

Strings in Java can be created using a constructor, for example, using double quotation marks ("):"

It would seem that these lines are completely the same, but there are a number of nuances that are often forgotten by novice Java developers. Let's look at an example:

To understand why this happens, and not otherwise you will help the article What is a string pool in Java? and Why are strings in Java unchanged.

Don't know the difference between Hashtable or HashMap?

One of the key differences is that it is synchronized. Therefore, in most cases, you need to use , rather than . I wrote about this in detail in a separate article.

HashtableHashMapHashtableHashMapHashtable

Don't know the difference between Set or Set<?>

Programmers who are just starting out in Java don't know the difference between Set and Set<?>. This also applies to other collections. Read more in this article.

Do not use the correct access levels

Very often, developers use the public access modifier for class fields. Yes, in this case it is very convenient and easy to get the value of the field, but this is considered bad form in Java development. Give the class fields the access level as low as possible.

That concludes my selection. If you have something to add to the popular errors of novice (and not so) Java developers, I am waiting for you in the comments :).