2

I'm just preparing for the Oracle Certified Professional Java SE 8 exam and I have some problems with generics and wildcards. I pretty well know that the code I'm submitting doesn't make a lot of sense and would not be written this way in 'real life' but it's an exercise for me.

  1. First line: ((ArrayList<Exception>)exceptions).add(new Exception()); compiles

How can this one compile? exceptions is an ArrayList containing FileNotFoundException objects (or child classes). How can I add an Exception to an ArrayList supposed to hold only elements that are a subclass of FileNotFoundException? Without the cast the line doesn't compile. But even with the cast, exceptions should still be unable to contain superclasses of FileNotFoundException. Am I right or do I misunderstand something?

  1. Line 2: ((ArrayList<IOException>)exceptions).add(new Exception()); doesn't compile

The difference between line 1 and 2 is that I now cast to an IOException, not an Exception. Exception is too 'wide' to be cast to IOException, sure. This should be the reason this line (same for line 5) does not compile. Right?

  1. Line 3: ((ArrayList<IOException>)exceptions).add(new IOException());

Compiles and I don't really understand why. Basically the same question as for line 1. IOException is superclass to FileNotFoundException, how can I put a member of a superclass in this ArrayList?

  1. Line 4: ((ArrayList<Exception>)exceptions).add(new FileNotFoundException());

Compiles. Same problem as Line 1.

package javaapplication;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.FileNotFoundException;

public static void main(String[] args) {
  List<? extends Throwable> exceptions = new ArrayList<FileNotFoundException>();

  ((ArrayList<Exception>)exceptions).add(new Exception());//1
  ((ArrayList<IOException>)exceptions).add(new Exception());//2,does not compile
  ((ArrayList<IOException>)exceptions).add(new IOException());//3
  ((ArrayList<Exception>)exceptions).add(new FileNotFoundException());//4
  ((ArrayList<FileNotFoundException>)exceptions).add(new
  Exception());//5,does not compile
}
1

2 Answers 2

1

That's why you get a "unsafe cast" compiler warning - because it's unsafe.

The answer to all your questions is badically:

  1. when you cast the list, it's treated as that type for that invocation
  2. you won't get an exception when putting the wrong type into a list, but you will get a ClassCastException when you take that object out of the list and assign it to a variable of the type of the list
0

Casting a generic type to a "generic supertype" compiles because the list during runtime is just a list holding objects which are checked for being of type Exception at runtime (because it was cast to that); During runtime, there is no difference between ArrayList<FileNotFoundException> and ArrayList<Exception>, and the cast overrides the normal type-checking behavior. Thus, unlike e.g. C++ templates, references held by ArrayList<FileNotFoundException> are not actually "unable" to refer to Exception instances, since at runtime they're just generic "Object" references (pun intended).

List<? extends Throwable> means "a list of some sort of Throwable but I'm not quite sure exactly what": Therefore, initializing the reference with a ArrayList<FileNotFoundException> object is valid but is not terribly useful in real life: It means you can't add anything to the list because you "lose" what type it is allowed to contain after compile time.

On the other hand, if you wanted to be able to state "this list holds FileNotFoundException or some parent type thereof", you can define a ArrayList<? super FileNotFoundException> generic type. Thus, ArrayList<? super FileNotFoundException> exceptions = new ArrayList<IOException>(); is valid and would allow you to add FileNotFoundException or IOException but not e.g. Exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.