Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Raw types are an advanced topic in Java. It required a good understanding of parametrized classes but might still be confusing. Luckily, IDEs can help us when we get things wrong. In particular, the Eclipse IDE produces a warning to notify us about this.

In this tutorial, we’ll check the warning and the steps to mitigate the issue.

2. Raw Types

Let’s consider the following code:

List strings = new ArrayList();

The List and, subsequently, ArrayList are parametrized types. We can see it in the class declaration:

public interface List<E> extends Collection<E> {
    // class body
}

However, it’s called raw types when we use parameterized types without parametrization. This not only reduces the flexibility of our code but also might introduce subtle bugs. Although in some cases, we’re forced to use raw types, mainly for backward compatibility, in general, it’s considered a bad practice.

3. Eclipse Static Analysis

Eclipse IDE complains about raw types and highlights the problematic parts of the code:

Eclipse-Highlighs-2

If we hover a cursor over the highlighted code, we’ll see the following popup:

References-to-generic-types-should-be-parameterized

This way, Eclipse helps us to ensure that the code we’re writing doesn’t contain mistakes. It’s especially useful at the beginning of a career. Additionally, it provides a menu with quick fixes. This way, we can easily resolve the problems.

Let’s parametrize the list to avoid the warning:

Eclipse-Highligh-Fix

From Java 5, we don’t need to add parametrization on both sides, and we can use a diamond operator. This is especially useful for long names and parametrizing with several types.

4. Conclusion

In this article, we discussed the Eclipse IDE process of issuing a “Raw type” popup to draw our attention to the incorrect use of parametrized classes. This popup offers quick fixes to the problem, which can help us resolve the issues faster.

IDEs and static analysis tools help us write cleaner code and avoid obvious pitfalls. Generics is one of the more advanced topics, and IDEs help identify subtle issues.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.