Here are some of the most common Java coding mistakes developers make, along with examples and explanations:
1. Null Pointer Exceptions
Mistake: Not checking for null before accessing an object’s methods or properties.
public class NullExample { public static void main(String[] args) { String str = null; System.out.println(str.length()); // Throws NullPointerException } }
Fix: Always check for null or use Optional in Java 8+.
if (str != null) { System.out.println(str.length()); }
2. Forgetting to Override equals() and hashCode()
Mistake: Using objects as keys in HashMap without proper equals() and hashCode().
class Person { String name; Person(String name) { this.name = name; } } public static void main(String[] args) { Map<Person, String> map = new HashMap<>(); map.put(new Person("John"), "John"); System.out.println(map.get(new Person("John"))); // Returns null }
Fix: Override both methods:
@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(name, person.name); } @Override public int hashCode() { return Objects.hash(name); }
3. Memory Leaks
Mistake: Not closing resources or holding object references unnecessarily.
public class MemoryLeak { private static List<Double> list = new ArrayList<>(); public void populateList() { for (int i = 0; i < 10000000; i++) { list.add(Math.random()); } } }
Fix: Use try-with-resources for closable objects and avoid static collections.
try (FileInputStream fis = new FileInputStream("file.txt")) { // use the stream }
4. Concurrency Issues
Mistake: Not synchronizing access to shared resources.
class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } // When used by multiple threads, count may not be accurate
Fix: Use synchronization or atomic variables.
class Counter { private AtomicInteger count = new AtomicInteger(0); public void increment() { count.incrementAndGet(); } public int getCount() { return count.get(); } }
5. Using == Instead of equals()
Mistake: Comparing objects with == instead of equals().
String str1 = new String("Hello"); String str2 = new String("Hello"); if (str1 == str2) { // false System.out.println("Equal"); }
Fix: Use equals() for object comparison.
if (str1.equals(str2)) { // true System.out.println("Equal"); }
6. Ignoring Exceptions
Mistake: Empty catch blocks or too broad exception handling.
try { // some code } catch (Exception e) { // empty or just e.printStackTrace(); }
Fix: Handle exceptions properly.
try { // some code } catch (SpecificException e) { log.error("Error occurred", e); // recovery or rethrow }
7. String Concatenation in Loops
Mistake: Using + operator for string concatenation in loops.
String result = ""; for (int i = 0; i < 1000; i++) { result += i; // Creates many intermediate String objects }
Fix: Use StringBuilder for multiple concatenations.
StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000; i++) { sb.append(i); } String result = sb.toString();
References
- Oracle Java Code Conventions
- Effective Java by Joshua Bloch
- Common Java Pitfalls – Baeldung
- Java Programming Mistakes – DZone
Visual Summary
Remember that many of these mistakes can be caught early by:
- Using static code analysis tools (SonarQube, FindBugs)
- Writing unit tests
- Conducting code reviews
- Using modern IDEs with code inspection features
Awesome
Good
Awesome
Very good