Note: test.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details

Viewed 98

Simple java program that has a ArrayList with few elements. On compiling I got the following error: Note: test.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

import java.util.*;
public class test{
    public static void main(String[] args){
        ArrayList obj = new ArrayList<>();
        obj.add("A");
        obj.add("B");
        obj.add("C");
        obj.add(1,"D");
        System.out.println(obj);
    }
}
1 Answers

As @MarsAtomic said, in the comment, it's a warning, not an error. After that, I'd want to help you understand why the compiler gives you that warning.

As the ArrayList is a class that uses generics, which allows the class to be a container that holds objects of the type you need, you are expected to specify a fixed type to be used, for example ArrayList<String> myList = new Arraylist<>(); In the other hand, if you don't specify the type, the compiler will use the type Object and it's considered that your list will work with "raw" type of data missing all complier checks for type-safe.

I hope it helps you!

Related