Questions tagged [unbounded-wildcard]

An unbounded wildcard is the type argument "?", a feature of generics in the Java language. This type argument represents some "unknown" type which was once present.

An unbounded wildcard is the type argument ?, a feature of generics in the language. This type argument represents some "unknown" type which was once present.

A wildcard is useful as a type argument in a situation where knowing the exact type is unnecessary:

public static void printElements(Iterable<?> anyIterable) {
    for (Object element : anyIterable) {
        System.out.println( element );
    }
}

A type parameterized with a wildcard places restrictions on how an object can be used:

List<String> listOfString =
    new LinkedList<>( Arrays.asList("a String") );

// We can assign any List to a List<?>, but, in
// doing so, we lose information about its original
// type argument.
List<?> listOfUnknown = listOfString;

// The wildcard therefore causes methods that once
// returned String to now return Object.
Object unknown0 = listOfUnknown.get( 0 );

// Compiler error:
//  The wildcard prevents us from passing arguments
//  to any method that once accepted String.
listOfUnknown.add( new Object() );

// Compiler error:
//  Since we have lost knowledge of the original
//  type, we can not pass String to the List either.
//  (This is the case, even though we, as the programmer,
//  can see this would be safe to do.)
listOfUnknown.add( "another String" );

The ? extends Object is equivalent to the unbounded wildcard.

See also:

58 questions
4
votes
1 answer

Java compilation error using a generic lambda for a method parameter with an unbounded wildcard type

I'm currently using a library method that takes a functional interface with a generic wildcard type as a method parameter (specifically, RecursiveComparisonAssert.withEqualsForFields​(BiPredicate equals, String... fieldLocations) in the…
M. Justin
  • 14,487
  • 7
  • 91
  • 130
4
votes
1 answer

Cast constant generic with wildcard

I'm doing static util method which returns completed future with empty optional: public class CompletableFutureUtils { private static final CompletableFuture> EMPTY_FUT = completedFuture(Optional.empty()); private static final…
radistao
  • 14,889
  • 11
  • 66
  • 92
4
votes
2 answers

Why simple “capture of ?” does not compile even type-safety could be compile-time inferred?

I have a class with strict, simple generic type: public class GenericTools { private final Supplier supplier; private final Consumer consumer; public GenericTools(Supplier supplier, Consumer consumer) { …
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
4
votes
2 answers

Java - Is new Stack[N] equivalent to new Stack[N] for generic data type Stack?

Is new Stack[N] equivalent to new Stack[N] for a generic data type Stack? EDIT: While I understand that mixing generic types and arrays should best be avoided and that more robust solutions exist, my query still stands: widely recognized…
Fabian
  • 191
  • 1
  • 9
4
votes
6 answers

difference between List and List

I've read alot about this, and I know that: List listOfObject = new ArrayList(); // (0) //can only work for TYPE == Object. //if TYPE extends Object (and thus objects of type TYPE are Objects), //this is not the same with Lists:…
joost
  • 71
  • 8
3
votes
3 answers

How to add an entry with an Integer value into Map

I have to place an integer value into the map below. Map map map.put("key",2000); When I run the above code I'm getting the following error: incompatible types: java.lang.Integer cannot be converted to capture#1 of ?
Ganesh
  • 91
  • 2
  • 9
3
votes
2 answers

Java language specification on wildcards

I am going through this link (Chapter 4. Types, Values, and Variables) and did not understand below point: The relationship of wildcards to established type theory is an interesting one, which we briefly allude to here. Wildcards are a restricted…
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
3
votes
1 answer

How to call wildcard java function from scala

I have a java library with some generic containers: public interface IColumnTable> { } public interface IColumn, M extends IMetaData> { } public interface IColumnValues { } public interface…
3
votes
2 answers

Java Generic For Parameterized Class : Unbounded Wildcards vs Raw Type

I have a parameterized interface RestHandler. public interface RestHandler { blah blah blah... } And I need to create a class from config using Class.forName. Now I come up with three version , which ALL compiles successfully. Version…
3
votes
2 answers

Using an Object as argument on unbounded wildcard reference object

Simple class: class Box { private T t; public Box(T t) { this.t = t; } public void put(T t) { this.t = t; } } trying to execute put() method passing an instance of Object Box box = new…
Łukasz
  • 1,980
  • 6
  • 32
  • 52
3
votes
1 answer

Incompatible wildcard types that should be compatible

Following on from this question, which provides a solution but doesn't explain it (unfortunately, the links in the answers are now dead): Take the following method: void method(Map myMap) { Set> set = myMap.entrySet(); …
thecoop
  • 45,220
  • 19
  • 132
  • 189
3
votes
2 answers

Generics Java, unbounded wildcard

Hi directly from a java tutorial provided by Oracle http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html static void filter(Collection c) { for (Iterator it = c.iterator(); it.hasNext(); ) if…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
2
votes
0 answers

Is casting from Wildcard to a specific generic parameterized type an example of capture conversion in Java?

For the following code that compiles without error and runs without exception: public class Main { public static void main(String[] args) { List unbounded = new ArrayList(); List strList = (List)…
Hill Tezk
  • 151
  • 4
2
votes
0 answers

casting from List to List using intermediate wildcard - safe?

I've noticed following java behavior, which confuse me a little: import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { new Test().test(); } public void test() { …
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
2
votes
1 answer

Why scala is unable to infer wildcard type from 2 functions?

I have the following function defined: import org.apache.spark.sql.catalyst.{ScalaReflection} import ScalaReflection.universe import universe.TypeTag def scalaTypesFor(dataType: DataType): Set[TypeTag[_]] = ... def scalaTypeOpt:…
tribbloid
  • 4,026
  • 14
  • 64
  • 103