Top Mistakes of Beginner Java Developers: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Top Mistakes of Beginner Java Developers: From A to Z

Java is a powerful but strict language. Beginners often stumble on the same pitfalls that experienced developers avoid automatically. In this article, we will analyze the 10 most common mistakes that beginner Java programmers make. You will learn not only why they occur, but also how to fix them using correct patterns and best practices.



1. Forgotten NullPointerException (NPE)

The most popular error in Java. It occurs when you try to call a method or access a field of an object that equals null.



Problem

public class User {    private String name;        public int getNameLength() {        return name.length(); // NPE if name == null    }}


Solution

Always check objects for null before use. Use Optional or @Nullable / @NonNull annotations.



public int getNameLength() {    return Optional.ofNullable(name)                   .map(String::length)                   .orElse(0);}


2. Ignoring the equals() and hashCode() Contract

If you override equals(), you must also override hashCode(). Otherwise, your objects will not work correctly in HashSet, HashMap, and other collections.



Problem

public class Person {    private String id;        @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        Person person = (Person) o;        return Objects.equals(id, person.id);    }    // hashCode() is not overridden!}

// In HashMap, two "equal" objects will end up in different bucketsMap<Person, String> map = new HashMap<>();map.put(new Person("1"), "Alice");System.out.println(map.get(new Person("1"))); // null


Solution

Always use Objects.hash() to generate the hash code:

@Overridepublic int hashCode() {    return Objects.hash(id);}


3. Incorrect Use of Generics (Raw Types)

Using raw types deprives you of compile-time type checking. This leads to ClassCastException at runtime.



Problem

List list = new ArrayList(); // raw typelist.add("string");list.add(123); // compiler does not complain

String s = (String) list.get(1); // ClassCastException!


Solution

List<String> list = new ArrayList<>();list.add("string");// list.add(123); // compilation error — great!String s = list.get(0); // safe


4. Mutating Objects in Collections

Changing the fields of an object that is already in a HashSet or is a key in a HashMap breaks the data structure. The object's hash code changes, but the bucket remains the same.



Problem

Set<StringBuilder> set = new HashSet<>();StringBuilder sb = new StringBuilder("A");set.add(sb);sb.append("B"); // mutationSystem.out.println(set.contains(sb)); // may be false!


Solution

Use immutable objects (String, Integer, record) as keys. If you need to change an object — remove it from the collection, modify it, and then add it back.



5. Incorrect Exception Handling

Two extremes: either swallowing exceptions (empty catch) or throwing too general exceptions (throws Exception).



Problem

try {    // dangerous code} catch (Exception e) {    // do nothing — error is lost}

                
                

Blogs

Book Recommendations