[Solved] uses unchecked or unsafe operations. recompile with -xlint:unchecked for details.

In this post, we will see about warning message uses unchecked or unsafe operations. recompile with -xlint:unchecked for details in java.

What is warning message: uses unchecked or unsafe operations

uses unchecked or unsafe operations is displayed when you compile code which compiler considers to be lacking in error checking or unsafe in some way. It’s a warning, not an error, and will not prevent you from compiling the code.

You will generally get this warning when you are using collection without using type specifier (e.g. ArrayList() instead of ArrayList) in your code.

Example:
Let’s say you have simple java code to print ArrayList.

When you will compile the code, you will get below output:

C:\Users\Arpit\Desktop\javaPrograms>javac PrintArrayListMain.java
Note: PrintArrayListMain.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

As you can see, compile gives us warning message that PrintArrayListMain.java uses unchecked or unsafe operations.

We are getting this warning message because we did not use generics with ArrayList.

If you want to know about unchecked or unsafe operations, you can use below command:

C:\Users\Arpit\Desktop\javaPrograms>javac PrintArrayListMain.java
Note: PrintArrayListMain.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Users\Arpit\Desktop\javaPrograms>javac PrintArrayListMain.java -Xlint:unchecked
PrintArrayListMain.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
countryList.add(“India”);
^
where E is a type-variable:
E extends Object declared in class ArrayList
PrintArrayListMain.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
countryList.add(“China”);
^
where E is a type-variable:
E extends Object declared in class ArrayList
PrintArrayListMain.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
countryList.add(“Bhutan”);
^
where E is a type-variable:
E extends Object declared in class ArrayList
3 warnings

How to resolve warning message: uses unchecked or unsafe operations.

You can resolve this warning message by using generics with Collections.

In our example, we should use ArrayList<String> rather than ArrayList().

When you will compile above code, you won’t get warning message anymore.

C:\Users\Arpit\Desktop\javaPrograms>javac PrintArrayListMain.java

That’s all about how to fix uses unchecked or unsafe operations. recompile with -xlint:unchecked for details in java.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.