Top 10 Most Common Java Mistakes
Top 10 Most Common Java Mistakes:
1. Neglecting Existing Libraries
It’s definitely a mistake for Java Developers to ignore the innumerable amount of libraries written in Java. Many of them have been polished over the years of their existence and are free to use. These could be logging libraries, like logback or network related libraries, like Netty.
2. Missing the ‘break’ Keyword
These Java issues can be very embarrassing, and sometimes remain undiscovered until run in production. However, missing a “break” keyword when such behavior is not desired can lead to disastrous results.
3. Forgetting to Free Resources
Every time a program opens a file or network connection, it is important for Java beginners to free the resource once you are done using it.
4. Memory Leaks
Problems with memory allocations are always possible. When a program creates references to objects that are not needed anymore, it will not be freed. In a way, we can still call this memory leak. The common reason is everlasting object references, because the garbage collector can’t remove objects from the heap while there are still references to them.
5. Excessive Garbage Allocation
Excessive garbage allocation may happen when the program creates a lot of short-lived objects. The garbage collector works continuously, removing unneeded objects from memory, which impacts applications’ performance in a negative way.
6. Null References without Need
Avoiding over use of null is a good practice.
7. Ignoring Exceptions
It is often tempting to leave exceptions unhandled. Exceptions are thrown on some purpose, so we need to address the issues causing these exceptions.
8. Concurrent Modified Exception
This exception occurs when a collection is modified while iterating over it using methods other than those provided by the iterator object.
9. Breaking Contracts
The contract contains two rules:
- If two objects are equal — their hash codes should be equal.
- If two objects having the same hash code — they may or may not be equal.
10. Using Raw Type
Raw types are types that are non-static members of class M that are not inherited from the superclass or superinterface of M. No alternatives to raw types until JAVA generic types were introduced in Java.