What Causes Javac to Issue the "Uses Unchecked or Unsafe Operations" Warning

What causes javac to issue the uses unchecked or unsafe operations warning

This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist() instead of ArrayList<String>()). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.

To get rid of the warning, you need to be specific about what type of objects you're storing in the collection. So, instead of

List myList = new ArrayList();

use

List<String> myList = new ArrayList<String>();

In Java 7 you can shorten generic instantiation by using Type Inference.

List<String> myList = new ArrayList<>();

uses unchecked or unsafe operations, Recompile with -Xlint:unchecked for details

This warning occurs when compiler cannot ensure the type safety in general.

If you do what it suggests and recompile with the -Xlint:unchecked switch, it will give you more detailed information.

An unchecked cast can cause this warning.

If you think your code is correct then simply suppress this warning by using

@SuppressWarnings("unchecked")
public void yourfunction() {
//you code that causes this warning
}

Compiler Warning class uses unchecked or unsafe operations

you get the warning here I think:

final JComboBox b = new JComboBox(values);

to avoid this you have the following possibilities:

add SupressWarning above the JComboBox

    @SuppressWarnings("unchecked")
final JComboBox b = new JComboBox(values);

or add SupressWarning above your method

   @SuppressWarnings("rawtypes")
public AddItem ( )

or my favourite add the generic:

final JComboBox<Object> b = new JComboBox<Object>(values);

compile warning: uses unchecked or unsafe operations

The field instanceHelper is a raw type of InstanceHelper. You should use the type parameter T:

InstanceHelper<T> instanceHelper;

You can remove the casting to T once that's complete:

for (int i = list.size(); i < toIndex; i++) {
list.add(instanceHelper.createInstance());
}

JavaFX Compilation Warning - uses unchecked or unsafe operations - Raw Data Types?

Your code is perfectly safe to run (it has bugs, which you'll discover when you run it, but those are unrelated to the typesafety about which you're asking).

Basically, the issue is that the addAll() method you are invoking takes a "varargs" parameter whose type is a generic type. For a TableView<S>, TableView.getColumns() returns an ObservableList<TableColumn<S,?>> and the ObservableList<E> addAll() method you are then calling takes a vararg parameter: addAll(E... elements). So you end up calling a method with signature addAll(TableColumn<Robot, ?>... columns).

Varargs are processed by converting them to an array, so you implicitly create an array of type TableColumn<Robot, ?>[] when you call the addAll method. Because arrays and parameterized types do not play well together, the compiler is unable to ensure the type safety of this array. Consequently, it issues a warning. See, e.g. here for an example of why the compiler can't ensure type safety here.

The authors of the addAll() method probably should have coded it so that it ensured type safety and annotated it so that it avoided this warning (if that's possible: I'd need to think about it for more time than I have right now).

To avoid the warning, you can simply invoke the add method twice:

robotTable.getColumns().add(colName);
robotTable.getColumns().add(colIntelligenge);

Alternatively you can create a List of table columns and pass that to the (other) addAll(...) method that takes a collection, instead of a varargs parameter:

robotTable.getColumns().addAll(Arrays.asList(colName, colIntelligence));

Unsafe or unchecked operations warning

In this case that warinig is shown because you are parsing directly the result of

input.readObject();

That returns an Object of type Object, (that is so generic), into an ArrayList, and the compiler tries to say you that, it could be any other type of objects.

In my opinion its not an important warning if that instruction in your code is always returning ArrayList, so i would add to your method definition.

@SuppressWarnings("unchecked")

Arraylist : uses unchecked or unsafe operations

The type of results.values is apparently Object, so casting it to an ArrayList<Sound> is dangerous since its run-time type could be incompatible with that type. If result.values is always an ArrayList<Sound> and you have the ability to change its type, then simply change it to ArrayList<Sound>. If you absolutely don't have the ability to change the type and you are absolutely certain that the cast will be valid at run-time, then you can add @SuppressWarnings("unchecked") above that line to suppress the warning.

I don't know what the type of names is in your excerpt, but it's likely that its compareTo() method has a similarly unsafe cast, due to underspecifying the parameter type. Look for a problem similar to this question.



Related Topics



Leave a reply



Submit