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
39
votes
5 answers

Difference between an unbound wildcard and a raw type

I was reading about generics and I did not understand the need for unbound wildcards and how it differs from raw type. I read this question but still did not get it clearly. In the Java tutorial page for unbound wildcard I got below two points and…
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110
29
votes
2 answers

Java nested generic type

How come one must use the generic type Map> instead of a simpler Map> for the following test() method? public static void main(String[] args) { Map> mappy = new HashMap
Louis Bliss
  • 393
  • 1
  • 3
  • 7
28
votes
3 answers

Cannot convert from List to List>

A raw list converts to List just fine. Why can't a list of raw lists convert to a list of List? { // works List raw = null; List wild = raw; } { // Type mismatch: cannot convert from List to List> List
djeikyb
  • 4,470
  • 3
  • 35
  • 42
25
votes
5 answers

What is the difference between Collection and Collection

I am mainly a C# developer and I was teaching Data Structures to my friend and they use Java in their University and I saw such an expression in Java: void printCollection(Collection c) { for (Object e : c) { System.out.println(e); …
Tarik
  • 79,711
  • 83
  • 236
  • 349
20
votes
3 answers

In Java, what can a wild card do that regular generics cannot do?

I am new to Java. In this document they give this as a use case for using wildcard: static void printCollection(Collection c) { Iterator i = c.iterator(); for (int k = 0; k < c.size(); k++) { System.out.println(i.next()); …
developer747
  • 15,419
  • 26
  • 93
  • 147
17
votes
4 answers

Irregularities with the (?) wildcard generic type

I believe that the type ? in generics is a specific unknown type. Which means, declaring let's say a list of that type would prevent us from adding any type of object into it. List unknownList; unknownList.add(new Object()); // This is an…
Codebender
  • 14,221
  • 7
  • 48
  • 85
16
votes
4 answers

Why can't I use the wildcard (?) as type of parameter, field, local variable, or as return type of a method?

The Oracle doc about Wildcards in generics says, The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more…
Solace
  • 8,612
  • 22
  • 95
  • 183
9
votes
2 answers

List> and List are incompatible types in java

I did not get this code to compile either way: List a = new ArrayList(); List> b = new ArrayList(); a = b; // incompatible types b = a; // incompatible types It seems that java does not consider List and List to be the same type…
Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
9
votes
3 answers

Java Method Unbounded Type or Class Return

I was reading this article, , about subclassing a builder class. I understood the article but there was one small bit that bothered me. There was this method, public static Builder builder() { return new Builder2(); } When I changed…
thlim
  • 2,908
  • 3
  • 34
  • 57
8
votes
1 answer

filtering a stream changes its wildcard bounds?

the below method compiles without problems: static Stream> getNumbers(Stream numbers) { return numbers.map(Optional::of); } yet if I add a simple filtering to it like this: static Stream
morgwai
  • 2,513
  • 4
  • 25
  • 31
8
votes
3 answers

What is the purpose of List if one can only insert a null value?

Based on the information provided in the link, it says that: It's important to note that List and List are not the same. You can insert an Object, or any subtype of Object, into a List. But you can only insert null into a…
kittu
  • 6,662
  • 21
  • 91
  • 185
6
votes
3 answers

Wildcard and type pameter bounds in java

Consider this case: class A {} class B { B b; B b2; } As I understand type bounds, in this case effective upper bounds of both T and E is class A. So the question: why javac doesn't accept…
Sergey94
  • 78
  • 7
6
votes
1 answer

AssertJ `containsExactly` assertion on list with wildcard

I have a getter returning a List with a wildcard: import java.util.List; public interface Foo { List getList(); } Where Bar is an other interface. When I write an assertion with AssertJ like…
Jmini
  • 9,189
  • 2
  • 55
  • 77
6
votes
1 answer

Unbound Wildcard Type

I was playing around in the Scala REPL when I got error: unbound wildcard type. I tried to declare this (useless) function: def ignoreParam(n: _) = println("Ignored") Why am I getting this error? Is it possible to declare this function without…
marstran
  • 26,413
  • 5
  • 61
  • 67
5
votes
0 answers

Strange behavior of wildcards in method call

I was going through wildcard topic in Java where i got stuck at this code below static void type(List list){ //... } static void type2(List list){ //... } If you call these methods by this code,…
abhi_awake
  • 186
  • 1
  • 8
1
2 3 4