While reading the Java official tutorial about generics, I found that you can restrict the type argument (in this case is T
) to extend a class and/or more interfaces with the 'and' operator (&
) like this:
<T extends MyClass & Serializable>
I replaced the &
with ,
(by mistake and still works, with a minor warning).
My question is, is there any difference between these two:
<T extends MyClass & Serializable>
<T extends MyClass , Serializable> // here is with comma
And the example method:
static <T extends MyClass & Serializable> ArrayList<T> fromArrayToCollection(T[] a) {
ArrayList<T> arr = new ArrayList<T>();
for (T o : a) {
arr.add(o); // Correct
}
return arr;
}
The difference between two things is the way in which they are unlike each other.
The most common use for among is when something is in or with a group of a few, several, or many things. The most common use of between is when something is in the middle of two things or two groups of things. It is sometimes used in the phrase in between.
To differ means to be different. To differentiate means to make (someone or something) different in some way.
<T extends MyClass & Serializable>
This asserts that the single type parameter T
must extend MyClass
and must be Serializable
.
<T extends MyClass , Serializable>
This declares two type parameters, one called T
(which must extend MyClass
) and one called Serializable
(which hides java.io.Serializable
— this is probably what the warning was about).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With