Top 10 Java Mistakes Every Developer Should Avoid

This article lists and explains ten of the most common errors Java developers make—from misusing Arrays.asList and raw types to incorrect loop deletions, mutable versus immutable objects, and constructor pitfalls—providing correct code examples and best‑practice recommendations.

Programmer DD
Programmer DD
Programmer DD
Top 10 Java Mistakes Every Developer Should Avoid

Reading Index

Array to ArrayList

Check if an array contains a value

Delete an element from a List inside a loop

HashTable vs. HashMap

Using raw collection types

Access level

ArrayList vs. LinkedList

Mutable vs. immutable

Parent and child constructors

"" vs. constructor

Future work

This list summarizes ten of the most frequently made mistakes by Java developers.

Array to ArrayList

Developers often write:

List<String> list = Arrays.asList(arr);
Arrays.asList()

returns a fixed‑size list implemented as a private static inner class of java.util.Arrays. It supports set(), get(), and contains() but does not support adding elements (calls to add() throw UnsupportedOperationException). To create a true java.util.ArrayList you should write:

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr));

The constructor accepts any Collection, and the list returned by Arrays.asList() already implements that interface.

Check if an array contains a value

Developers sometimes convert the array to a Set:

Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);

This is unnecessary. A simpler and more readable solution is:

return Arrays.asList(arr).contains(targetValue);

Or using a manual loop:

for (String s : arr) {
    if (s.equals(targetValue)) {
        return true;
    }
}
return false;

The first method is generally preferred for readability.

Delete an element from a List inside a loop

Removing elements by index while iterating causes index shifting and can skip elements. For example:

ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
for (int i = 0; i < list.size(); i++) {
    list.remove(i);
}
System.out.println(list); // prints [b, d]

This approach is error‑prone. Using an Iterator is the correct way:

Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
    String s = iter.next();
    if (s.equals("a")) {
        iter.remove();
    }
}

Calling next() before remove() avoids ConcurrentModificationException that occurs with a foreach loop.

HashTable vs. HashMap

From an algorithmic perspective, a hash table is a data structure; in Java the modern implementation is HashMap. Hashtable is synchronized, so HashMap is usually preferred.

Using raw collection types

Raw types bypass generic type checking and can cause runtime ClassCastException. Example:

public static void add(List list, Object o) {
    list.add(o);
}

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    add(list, 10); // adds an Integer
    String s = list.get(0); // ClassCastException
}

Using generics prevents such unsafe casts.

Access level

Exposing fields as public makes them directly mutable from outside code, which is a poor design. Prefer the most restrictive access level that still satisfies the required functionality.

ArrayList vs. LinkedList

Both implement List, but they have different performance characteristics. When the workload involves many insertions and deletions with few random accesses, LinkedList is usually faster.

Mutable vs. immutable

Immutable objects are safe and simple but can create many short‑lived instances, increasing GC pressure. Mutable objects (e.g., StringBuilder) reduce intermediate object creation when building strings or aggregating results.

Parent and child constructors

If a superclass does not define a no‑argument constructor, the compiler will not generate one automatically. This leads to compilation errors when a subclass tries to call super(). The fix is to either add an explicit no‑arg constructor to the superclass or explicitly call an existing superclass constructor from the subclass.

Constructor error illustration
Constructor error illustration

"" vs. constructor

Two ways to create a String:

String x = "abc"; // literal
String y = new String("abc"); // constructor

Both produce equal content ( .equals() returns true), but literals are interned and may refer to the same object ( x == y is true for literals, false for objects created with new).

Future work

The list is based on analysis of many open‑source projects, Stack Overflow questions, and popular Google searches. It may not be exhaustive, and readers are encouraged to comment with additional common mistakes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CollectionsArrayListbest-practicescoding-errorsjava-mistakes
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.