Beginners · 548 words · 3 minute read

10 Mistakes Every Java Beginner Makes (and How to Fix Them)

By · Published 2026-02-18 · Updated 2026-08-24

Most beginner mistakes are reasonable predictions based on another language or on how code looks. The fix is to replace the prediction with a precise Java rule and a small test. Work through these in order; later mistakes build on the earlier ones.

1. Comparing String references with ==

For references, == asks whether both variables identify the same object. Use equals for String content. If null is possible, Objects.equals(a, b) handles it explicitly.

2. Losing the fraction in integer division

7 / 2 is evaluated as integer division and produces 3. Converting the already-computed result to double cannot restore the fraction. Make an operand floating point before division: (double) total / count.

3. Crossing an array boundary

An array of length n has indices 0 through n - 1. Use index < values.length, and decide what an empty array means before reading values[0]. Practice this in the array guide.

4. Expecting immutable String methods to edit the original

trim, replace, and toUpperCase return a String. Assign or return that value. For repeated edits, use StringBuilder and convert once at the end.

5. Putting every statement in main

Separate calculation from input and output. A method such as average(int[] values) can be tested with an empty, one-element, and ordinary array without simulating console input.

6. Making fields public or adding setters mechanically

An object should protect its invariant. If a balance cannot be negative, do not expose setBalance; expose validated operations that represent allowed changes. Continue with the OOP exercises.

7. Catching too broadly

Catch the specific failure you can handle around the operation expected to fail. An empty catch (Exception e) hides defects. Preserve the cause when translating a low-level exception and use try-with-resources for AutoCloseable resources.

8. Modifying a collection during enhanced iteration

Structural changes can invalidate a fail-fast iterator. Use the iterator’s remove method, removeIf, or build a new result. In concurrent code, select a collection and synchronization strategy designed for that access pattern.

9. Assuming collection order

HashMap and HashSet do not promise iteration order. Choose LinkedHashMap for encounter order or TreeMap for key order, and test the guarantee your output needs—not the order one run happened to produce.

10. Testing only the sample

One expected output demonstrates one path. Add boundary cases: empty input, one value, duplicates, negative values, maximum sizes, invalid text, or missing files as relevant. A solution is not general merely because a parameterized exercise accepts it.

Tested example: two easy-to-miss rules

public class Main {
    public static void main(String[] args) {
        String first = new String("java");
        String second = new String("java");
        System.out.println(first == second);
        System.out.println(first.equals(second));
        System.out.println(7 / 2);
        System.out.println(7 / 2.0);
    }
}

The output is:

false
true
3
3.5

A correction routine that sticks

  1. Predict the output before running the smallest reproducer.
  2. Run it in the Java compiler.
  3. Change one input so the incorrect mental model would predict a different result.
  4. Write the corrected rule in one sentence.
  5. Solve one related exercise from Core Java Basics, Strings, or Collections.

Avoid replacing these mistakes with rigid style slogans. “Methods must be under 20 lines,” for example, is not a Java rule. Prefer cohesive methods with clear inputs and outputs; length is only one signal that responsibilities may need separation.

Continue with the Java practice path · Try code in the Java compiler