Difference between Scala’s existential types and Java’s wildcard by example?

This is Martin Odersky’s answer on the Scala-users mailing list: The original Java wildcard types (as described in the ECOOP paper by Igarashi and Viroli) were indeed just shorthands for existential types. I am told and I have read in the FOOL ’05 paper on Wild FJ that the final version of wildcards has some … Read more

What is the difference between bounded wildcard and type parameters?

They expose different interfaces and contract for the method. The first declaration should return a collection whose elements type is the same of the argument class. The compiler infers the type of N (if not specified). So the following two statements are valid when using the first declaration: Collection<Integer> c1 = getThatCollection(Integer.class); Collection<Double> c2 = … Read more

Why can’t you have multiple interfaces in a bounded wildcard generic?

Interestingly, interface java.lang.reflect.WildcardType looks like it supports both upper bounds and lower bounds for a wildcard arg; and each can contain multiple bounds Type[] getUpperBounds(); Type[] getLowerBounds(); This is way beyond what the language allows. There’s a hidden comment in the source code // one or many? Up to language spec; currently only one, but … Read more

Java Generics (Wildcards)

In your first question, <? extends T> and <? super T> are examples of bounded wildcards. An unbounded wildcard looks like <?>, and basically means <? extends Object>. It loosely means the generic can be any type. A bounded wildcard (<? extends T> or <? super T>) places a restriction on the type by saying … Read more

Mockito: Stubbing Methods That Return Type With Bounded Wild-Cards

You can also use the non-type safe method doReturn for this purpose, @Test public void testMockitoWithGenerics() { DummyClass dummyClass = Mockito.mock(DummyClass.class); List<? extends Number> someList = new ArrayList<Integer>(); Mockito.doReturn(someList).when(dummyClass).dummyMethod(); Assert.assertEquals(someList, dummyClass.dummyMethod()); } as discussed on Mockito’s google group. While this is simpler than thenAnswer, again note that it is not type safe. If you’re concerned … Read more

Java Generics With a Class & an Interface – Together

Actually, you can do what you want. If you want to provide multiple interfaces or a class plus interfaces, you have to have your wildcard look something like this: <T extends ClassA & InterfaceB> See the Generics Tutorial at sun.com, specifically the Bounded Type Parameters section, at the bottom of the page. You can actually … Read more

tech