Recompile with –Xlint:unchecked - Java

I was recently working on a Java project for TAFE and I started getting these two warnings,

  • Note: Some input files use unchecked or unsafe operations
  • Note: Recompile with –Xlint:unchecked for details

So I did just that as shown below:

Javac *.java –d . -Xlint:unchecked

You will notice that even when you get these warnings while compiling, your class files will still be generated and your application will probably still work. These are just warnings for best practice programming and may cause your code to break in the future. After a recompile with the Xlint:unchecked flag set I was directed to the source of my errors, in this case I had been using an ArrayList which is a collection of Objects and by default accepts all objects. Best practice should be to declare your ArrayList of the highest type of object you intend to use within the ArrayList, it is unlikely that you will always need an ArrayList for all types of Objects and it also means you have to cast the objects when retrieving them from the collection. Below is an example on how to declare a ArrayList properly setting the Object type to String. Private ArrayList arraylist = new ArrayList(); If anyone else has come across the same problem then please feel free to post your solutions in the comments section below, if you are having problems with the same error then post in the comments and ill do my best to help. Ben

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.