Unchecked or unsafe operations error with ArrayList / PrimeDirective

I thought I’d drop this in here in case anyone ran into this, because this error is so obscure!

$ javac PrimeDirective.java 
Note: PrimeDirective.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I ran into this in the PrimeDirective exercise, which was tripping me up. It has to do with:

    ArrayList<Integer> primes = new ArrayList();

The issue here was that I wasn’t specifying the type in the instantiation. It should be:

    ArrayList<Integer> primes = new ArrayList<Integer>();

Types always mess me up! :sweat_smile: :sweat_smile: :sweat_smile:


You can actually leave the 2nd set of arrow brackets empty since Java 7 or so. The compiler will infer the type from the left side of the assignment.

ArrayList<Integer> primes = new ArrayList<>();

It’s funny because types are so difficult at first, but then it can be a shock after going back to weakly typed languages.