[Fixed] unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.

In this post, I will be sharing how to fix unchecked or unsafe operations. Recompile with -Xlint: unchecked for details in Java.

Read Also: [Solved] java uses or overrides a deprecated api. recompile with -Xlint:deprecation for details.

As always, we will first produce this warning "unchecked or unsafe operations. Recompile with -Xlint:unchecked for details" before moving on to the solution.

[Solved] unchecked or unsafe operations. Recompile with -Xlint: unchecked for details


 ðŸ’¡ Did You Know?

"unchecked or unsafe operations. Recompile with -Xlint:unchecked for details." is a warning not an error. This warning will not prevent you from compiling the code.

1. Producing the warning: unchecked or unsafe operations. Recompile with -Xlint: unchecked for details


You can easily produce this warning by using a Collection without using the type specifier. For example, in the below code we are using new ArrayList() instead of new ArrayList<String>(). Compiler considers new ArrayList() lacking in error checking or unsafe.

 import java.util.*;

public class UncheckedUnsafeWarning {
    public static void main(String args[]) {
     	// Creating object of ArrayList class
        ArrayList list = new ArrayList();	
	// Adding elements to the object
	list.add("Smartphone");
	list.add("Laptop");
	list.add("Tablet");
	list.add("Smartwatch");
	// Printing the ArrayList
	System.out.println(list);
    }
}


Output:

C:\Users\SUBMITTAL\Desktop\JavaPrograms>javac UncheckedUnsafeWarning.java
Note: UncheckedUnsafeWarning.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

What you should do

Append -Xlint:unchecked to the javac YOURCLASSNAME.java command to know more about the unchecked or unsafe operations as shown below in the example:

C:\Users\SUBMITTAL\Desktop\JavaPrograms>javac UncheckedUnsafeWarning.java -Xlint:unchecked

UncheckedUnsafeWarning.java:5: warning: [unchecked] unchecked conversion
                                   ArrayList<String> list = new ArrayList();
                                                                             ^
     required: ArrayList<String>
     found: ArrayList
     1 warning


Solution:

You can get rid of this warning message in two ways:

1. Using generics with Collection i.e. replacing new ArrayList() with new ArrayList<E>()

To get rid of the warning message and run the Java program, just replace the below line:
 ArrayList list = new ArrayList();


with

 ArrayList<String> list = new ArrayList<String>();

in the above code.

2. Adding @SuppressWarnings("unchecked") annotation to the main method [Not Recommended]

Add the @SuppressWarnings("unchecked") annotation to the main method as shown below in the above code:

     @SuppressWarnings("unchecked")
    public static void main(String args[]) {


You will not be receiving the warning message if you implement any one of the two solutions.

That's all for today. Please mention in the comments if you have any questions regarding how to fix unchecked or unsafe operations. Recompile with -Xlint:unchecked for details warning in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry