What does the unsafe operations compiler error mean?
What does this error mean?
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unc开发者_如何学Gohecked for details.
Any suggestions how to avoid that kind of error?
That's the error you get when you use Collections without specifying a type. You probably have something like:
ArrayList myList = new ArrayList(); // or some other Collection class
If that's the case, you need to change that to specify what type of objects you want to store. For example:
ArrayList<String> myList = new ArrayList<String>();
Read up on Java Generics for more information.
This is my best guess without seeing your code and the full error message. There could be other causes for that message, this is just the problem that I've seen accompany that message before.
First, recompile with -Xlint:unchecked to see what the problem is. Then fix those problems. There are a number of potential high-level causes for unchecked warnings. One is that you didn't provide type parameters where you should have. There are some situations where they are unavoidable, and then you can suppress the specific warning, but these are the exception, and care must be taken that you aren't suppressing warnings that are really important.
So recompile with -Xlint:unchecked and post additional questions if you have trouble with any of the specific issues that are revealed.
What does it mean?
Java generics allow you to write something like this:
List<String> l = ...;
String s = l.get(0); // note there is no explicit typecast.
But if the compiler tells you that your code has "unchecked or unsafe operations", it is saying that you have broken the rules for using generics safely, and your code may give runtime class cast exceptions at unexpected places; e.g. in the statement above where we left out the typecast.
There are a few things that will cause the compiler to complain about unchecked or unsafe operations, and each one requires a different remediation. Do what the compiler is telling you and run it with the -Xlint option.
精彩评论