0

4.5 of the Java SE states: "A generic type has type parameters F1,...,Fn with corresponding bounds B1,...,Bn. Each type argument Ti of a parameterized type ranges over all types that are subtypes of all types listed in the corresponding bound. That is, for each bound type S in Bi, Ti is a subtype of S[F1:=T1,...,Fn:=Tn] (§4.10)." I just finished reading this ENTIRE tutorial of generics and cannot for the life of me decipher what this paragraph is trying to say no matter how I look at it. It seems to me as though they are saying the type argument is a subtype of the type parameters bound, but this can't be true because a type argument may equal the bound.

Matthew S.
  • 464
  • 2
  • 12
  • It's saying that the possible values of the parameter _Ti_ are all types that are subtypes of every bound (so if the bounds are `extends Serializable` and `extends Comparable super Ti>`, then the _range_ of _Ti_ includes types such as `Integer` and `String` plus `MySerializableComparable`). – chrylis -cautiouslyoptimistic- Mar 20 '20 at 02:44
  • Yes but wouldn't the range include the types Serializable and Comparable as well? What I'm saying is the range isn't *just* the sub types of the bound, it's the bound as well. I don't get how what they're saying is true if what I'm saying is true – Matthew S. Mar 20 '20 at 12:20
  • No; the range is "all types that are subtypes of *all types listed*", not "any type listed". (Note that Foo is a subtype of itself.) – chrylis -cautiouslyoptimistic- Mar 20 '20 at 22:27
  • That's not what I meant. I'm saying a type argument is not only valid for the subtypes of a bound but that it's valid for the bound itself as well. For example if T extends Number the type arguments that are valid for T are not just Integer or Double or any other one of Number's sub classes but Number as well. I'm not saying a type parameter is valid for *any* type, I'm saying it's valid for it's bound and all of the classes that descend from it's bound. – Matthew S. Mar 21 '20 at 00:32
  • A type parameter with the bounds you provided as an example is valid for the types I mentioned in my first reply. – Matthew S. Mar 21 '20 at 00:36
  • Nope, the type would have to be _both_ `Serializable` and `Comparable`, not just either. That's the difference between "all" and "any". – chrylis -cautiouslyoptimistic- Mar 21 '20 at 01:08
  • I'm sorry, I'm still a newbie and did not check this on my compiler to verify and falsely assumed it could be one or the other. But even so, my question still stands, doesn't that mean the type parameter is valid for the bound as well as it's sub types? Not just the subtypes? That's what's throwing me off. If the example bound you provide is valid for the type Serializable that implements the interface that would mean it's range is not restricted to the subtypes only but the bound as well. I can't make sense of the phrasing – Matthew S. Mar 21 '20 at 01:28
  • 1
    See [section 4.10](https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.10). (Note that this is consistent with general mathematical usage; a set is a subset of itself, and if you want _only_ a smaller set, you say "proper subset".) – chrylis -cautiouslyoptimistic- Mar 21 '20 at 01:45
  • Oh snap! That's right! I forgot about that part, thank you so much! Will pay closer attention to terminology next time – Matthew S. Mar 21 '20 at 02:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210060/discussion-between-matthew-s-and-chrylis-on-strike). – Matthew S. Mar 21 '20 at 13:51

0 Answers0