In this post, we will see about warning message uses unchecked or unsafe operations. recompile with -xlint:unchecked for details
in java.
Table of Contents
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
Example:
Let’s say you have simple java code to print ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String[] args) { ArrayList countryList=new ArrayList(); countryList.add("India"); countryList.add("China"); countryList.add("Bhutan"); System.out.println(countryList); } } |
When you will compile the code, you will get below output:
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:
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()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.ArrayList; public class PrintArrayListMain { public static void main(String[] args) { ArrayList<String> countryList=new ArrayList<>(); countryList.add("India"); countryList.add("China"); countryList.add("Bhutan"); System.out.println(countryList); } } |
When you will compile above code, you won’t get warning message anymore.
That’s all about how to fix uses unchecked or unsafe operations. recompile with -xlint:unchecked for details
in java.