Search code examples
javagenericsinheritancemultiple-inheritanceseparator

"&" or ",": What is the difference between A<T extends I1 & I2> and A<T extends I1 , I2>


Multiple generic interface separator: "," or "&"

I was always using A<T extends I1, I2> but today, I saw A<T extends I1 & I2>. What is the difference between these two notation?

Does it have a different meaning? Purpose?


Solution

  • A<T extends I1, I2>
    

    is a type A with two parameters

    1. T which must be a subtype of I1
    2. I2

    You can read this as "A <preposition> type T which extends I1 <preposition> type I2", so SortedMap<Key extends comparable, Value> is a "map from comparable type key to type value"


    A<T extends I1 & I2>
    

    is a type A with a single parameter

    1. T which must be a subtype of I1 and a subtype of I2

    You can read this as "A <preposition> type T which extends I1 and I2" so a SortedSerializableSet<T extends Serializable & Comparable> can be read "a sorted set of elements of type T which is both serializable and comparable"