Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What does instanceof Collection<? extends Number> mean?

843793Aug 1 2003 — edited Aug 5 2003
Why is a Collection<? extends String> an instance of a Collection<? extends Number> ???
		Collection<? extends Number> ref1 = new LinkedList<Integer>();
		Collection<? extends String> ref2 = new ArrayList<String>();
		Collection<String>           ref3 = new ArrayList<String>();
		
		boolean b;
		// true
		b = ref1 instanceof Collection<? extends Number>;
		System.out.println(b);
		
		// >>>>>>  true !!!   <<<<<<
		b = ref2 instanceof Collection<? extends Number>;
		System.out.println(b);
		
		// error: inconvertible types
		// found   : java.util.Collection<java.lang.String>
		// required: java.util.Collection<? extends java.lang.Number>
		b = ref3 instanceof Collection<? extends Number>;
My understanding of Collection<? extends Number> is that it denotes a set of types ranging over all instantiations Collection<T> where T is a subtype of Number.

And the same for Collection<? extends String>. Since String is a final class this set of types has only one element, namely Collection<String>.

Both sets have no overlap. How can an object of a type in the set Collection<? extends String> be an instance of a type in the set Collection<? extends Number>?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 2 2003
Added on Aug 1 2003
14 comments
804 views