Introduction
Java is one of the most popular and in-demand programming languages. However, on the path to mastery, beginners often make typical mistakes that slow down development and lead to hard-to-find bugs. In this article, we will look at the 10 most common mistakes of novice Java developers. You will learn how to recognize them, fix them, and most importantly — avoid them in the future. The material will be useful for both students and those who are just starting their journey in Java.
1. Confusion between == and equals()
This is perhaps the most common mistake. The == operator compares object references (checks if variables point to the same object in memory), while the .equals() method compares the contents of objects.
Example of the mistake:
String a = new String("Hello");String b = new String("Hello");if (a == b) { System.out.println("Strings are equal");} else { System.out.println("Strings are NOT equal");}// Output: Strings are NOT equalHow to do it correctly:
if (a.equals(b)) { System.out.println("Strings are equal");}// Output: Strings are equalTip: Always use .equals() to compare strings, objects, and collections. Use == only for primitive types (int, char, boolean) or when you explicitly need to check that it is the same object.
2. Ignoring NullPointerException (NPE)
NullPointerException is the bane of all Java developers. It occurs when you try to call a method or access a field of an object that is null.
Example of the mistake:
public class User { String name; public int getNameLength() { return name.length(); // If name == null, NPE will occur }}How to avoid it:
- Always check objects for
nullbefore using them:if (name != null). - Use
Optional<T>(Java 8+) to explicitly indicate that a value may be absent. - Apply
@Nullableand@NotNullannotations (e.g., from Lombok or IntelliJ).
public int getNameLength() { return (name != null) ? name.length() : 0;}3. Incorrect exception handling
Beginners often either "swallow" exceptions (write an empty catch block) or use too generic catch (Exception e).
What you should NOT do:
try { // dangerous code} catch (Exception e) { // do nothing}How to do it correctly:
- Catch specific exceptions (e.g.,
IOException,SQLException). - In the
catchblock, log the error or at least print it to the console (e.printStackTrace()or via a logger). - Use try-with-resources for automatic resource closing (files, database connections).
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); }} catch (IOException e) { System.err.println("Error reading file: " + e.getMessage());}4. Forgetting about StringBuilder when concatenating strings in loops
Strings in Java are immutable. Every time you use the + operator for strings, a new String object is created. In a loop, this leads to a huge number of temporary objects and performance degradation.
Bad example:
String result = "";for (int i = 0; i < 1000; i++) { result += "number " + i + ", ";}Good example:
StringBuilder sb = new S