2

For the following code that compiles without error and runs without exception:

public class Main
{
    public static void main(String[] args) {
        List<?> unbounded = new ArrayList<String>();

        List<String> strList = (List<String>) unbounded;    // Is this a capture conversion?
        List<Integer> intList = (List<Integer>) unbounded;  // Is this a capture conversion?
    }
}

Is the above code snippet an example of Capture Conversion working in Java?

According to JLS,

There exists a capture conversion from a parameterized type G<T1,...,Tn> (§4.5) to a parameterized type G<S1,...,Sn>, where, for 1 ≤ i ≤ n :

  • If Ti is a wildcard type argument (§4.5.1) of the form ?, then Si is a fresh type variable whose upper bound is Ui[A1:=S1,...,An:=Sn] and whose lower bound is the null type (§4.1).

I comprehend the above statement as follows: If the wildcard is of the form '?', there exists a capture conversion from G<T> to G<S> as long as 'S' is within the bound of Object(upper bound) and null(lower bound).

So, for example in the above code snippet, converting 'List<?>' to 'List<Integer>' will be a capture conversion, which would not throw any runtime exception (which is also stated in JLS)? Is that correct? Also, is it correct to say that it doesn't throw runtime exception due to type erasure?

If that's the case, wouldn't capture conversion undermine type safety? According to JLS, capture conversion is introduced to make wildcards more useful without undermining the type system?

Hill Tezk
  • 151
  • 4
  • Your code contains no capture conversion, but unchecked casts. Check [the JLS](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.10) for actual examples, specifically the `reverse` method example. In a nutshell capture conversion means that you can treat a List of an unknown type `List>` as a list of a specific (but still unknown) type `List`. Why that's useful (and why it's safe) is explained in the JLS comment quite well. – Joachim Sauer Jun 09 '21 at 08:30
  • @JoachimSauer I see. I can also see the Compiler giving a warning about unchecked conversion. In this case, i find that I don't really understand what capture conversion is. As I read in the JLS (as quoted in my question description), it seems that the JLS is describing capture conversion as converting a wildcard form to a parameterized type. Could you point out what I misunderstand about the section that I quoted of JLS in the question description? – Hill Tezk Jun 09 '21 at 08:40
  • I have also read this post about capture conversion: [What is a capture conversion in Java and can anyone give me examples?](https://stackoverflow.com/questions/4431702/what-is-a-capture-conversion-in-java-and-can-anyone-give-me-examples). In the example given by the JLS, I saw that capture conversion occurs when passing a wildcard generic to a method with an argument of the form "List". Isn't that similar to the code example that I gave? Both converting a wildcard to a parameterized type? – Hill Tezk Jun 09 '21 at 08:46
  • I kind of feel there is a difference between my code and the example in JLS, but I couldn't really explain the difference and what really happens behind the scene. If would be best if someone could help me really understand capture conversion or point me to some more resources. – Hill Tezk Jun 09 '21 at 08:49
  • Capture conversion is something that happens "automatically" and is not an explicit cast. I can't really give a better example than the one you linked to. – Joachim Sauer Jun 09 '21 at 08:50

0 Answers0