Top Java common mistakes

Top Java common mistakes

Hello everyone, this is the first time I post something on HashNode. This article will talk about common mistakes with Java.

I was a little skeptical about what I wrote, whether it was too normal or I was just overreacting. However, I still write it here and hope it will help someone. Thanks.

1. Compare real numbers

In general, in most programming languages, comparing the results of real numbers should be avoided. Here's a very simple example.

System.out.println(0.1 + 0.2 == 0.3); // false

This problem has to do with the representation of real numbers in binary. In summary, always remember calculation with real numbers is always not really accurate and should be limited (although there are standard libraries that deal with this problem).

2. How to compare StringBuilder?

How do you check two String objects have the same content? Use equals() method instead of ==, right?

How about the same question but with StringBuilder?

StringBuilder a = new StringBuilder("Hello");
StringBuilder b = new StringBuilder("Hello");
a.equals(b); // true or false?

The result is false because the StringBuilder class does not implement its equals() method. So you need to call both's toString() before equals().

a.equals(b); // false
a.toString().equals(b.toString()); // true

3. Foreach with null Collection

Okay, this can be considered a best practice and is often said many times. In particular, you should return an empty collection instead of null.

Try this example.

List<Integer> primes = null;

// Yeah, we got a NullPointerException
for (Integer prime : primes)
    // Do something

Sometimes, I wonder why java doesn't simply skip the loop in this case. On the other hand, I find this quite good, when it helps us to write better code, just a bit annoying.

4. Use a primitive variable to iterate through a Collection

Have you ever code like this?

Collection<Integer> list = new ArrayList<>(List.of(2, 3, 5, 7));

// Iterate with foreach
for (int element : list)
    // Do something with element

// Or using iterator
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    int element =;
    // Do something with element

But what's wrong with the above code? Let's me explain, there are two points to remember:

  • Most Collection classes allow null element
  • Unboxing process may produce NullPointerException

Yeah, you got it. When Java tries to unbox a wrapper variable, it may get a NullPointerException.

Just limit the use of primitive variables and use wrapper types instead. Of course, it doesn't completely fix the error, we still need some additional null checks.

5. Forget check null with wrapper type

Some situations require you to do an explicit null check but are sometimes ignored. As mentioned above, the variable representing the elements of almost Collection can be contain null value. So make sure you keep this under control.

Another case is to use Boolean in the conditional expression but not check for null.

Boolean b = null;
if (b)

You get a NullPointerException for forgetting to do a null check.

if (b != null && b)
   // This is better

I'm tired of null checks. Better to do this.

if (b == Boolean.TRUE)
    // Now null check is unnecessary

6. Try to modify an immutable collection

When I first learned java, I used to code like this.

// Arrays.asList(), List.of(), etc
List<Integer> a = Arrays.asList(2, 3, 5, 7);
List<Integer> b = List.of(2, 3, 5, 7);

// And this will throw an exception

Until I got the UnsupportedOperationException error. This is easy to understand since the above static methods always return an unmodifiable collection.

Therefore, if I try to add or remove an element, the above exception is thrown.

List<Integer> a = new ArrayList<>(List.of(2, 3, 5, 7));

The code will be a bit longer but totally works.

Okay, that's all. Thank you for taking the time to read my post. If it is useful to you, please like or share.

Ps: I don't know if I should add the 7th mistake to this post. My article uses an image on Google as a cover. I wonder if that's legal, let me know in the comments. Thanks.