For a class I was assigned to write code to read objects of the class Vehicle
using ObjectInputStream (in
). The objects are stored in an ArrayList called orders
.
SSCE:
// Read all orders Object obj = in.readObject(); orders = (ArrayList<Vehicle>) obj;
However, the compiler complains:
MacBook:Homework Brienna$ javac Orders.java -Xlint:unchecked Orders.java:152: warning: [unchecked] unchecked cast orders = (ArrayList<Vehicle>) in.readObject(); ^ required: ArrayList<Vehicle> found: Object 1 warning
I always try to improve my code instead of ignoring or suppressing warnings. In this case, I have come up with a solution, but I’m trying to understand why it works, and if there is a better solution.
This update stops the warning:
// Read all orders, casting each order individually Object obj = in.readObject(); ArrayList ar = (ArrayList) obj; for (Object x : ar) { orders.add((Vehicle) x); }
Based on what I understand from what I’ve been reading, it works because (ArrayList<Vehicle>) obj
may throw an exception if not all the elements are Vehicle
. I am confused — non-Vehicle objects can be added to the ArrayList even if its type parameter has been specified as Vehicle
? Also, is there a better solution, e.g. using instanceof
?