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
0
votes
0 answers

Java : Generics, wildcards with multiple bounds

First of all, happy new year to everyone ! I would like to write a method with a multiple bounds parameter like that: public static void max(List> list) But unfortunately this code does not compile, could someone…
valoo86
  • 23
  • 2
0
votes
1 answer

What is the correct way to obtain the class object for class List without losing type information?

As a preface to my question, please note that I know what type erasure is. I feel the need to state this so useful answers don't get buried in basic explanations. I would like to obtain the class object for class List. For a non-generic type, I…
Martin Geisse
  • 1,189
  • 1
  • 9
  • 22
0
votes
0 answers

Unexpected unchecked cast warning produced by casting to a reified (unbounded wildcard) type

I had thought that casting to an unbounded wildcard type would never generate an "unchecked cast" warning because unbounded wildcard types are reified, and therefore the type information needed to safely complete the cast would be present at…
scottb
  • 9,908
  • 3
  • 40
  • 56
0
votes
1 answer

Making parameterized ScalaCache generic with runtime configuration

The git repo that contains the issue can be found here https://github.com/mdedetrich/scalacache-example The problem that I currently have is that I am trying to make my ScalaCache backend agnostic with it being configurable at runtime using typesafe…
0
votes
5 answers

Generics with optional multiple bounds, e.g. List

I have a method that should only accept a Map whose key is of type String and value of type Integer or String, but not, say, Boolean. For example, map.put("prop1", 1); // allowed map.put("prop2", "value"); // allowed map.put("prop3", true); //…
Somu
  • 3,593
  • 6
  • 34
  • 44
0
votes
2 answers

Convert method override with wildcard from Java to Scala

I have a Java method signature that I can't seem to convert to a signature in Java. Here is the Java code: public class InjectorListCellRenderer extends DefaultListCellRenderer { public Component getListCellRendererComponent(JList list, Object…
KeyboardDrummer
  • 577
  • 5
  • 21
0
votes
1 answer

Java: Trouble with wildcards/generics

I am really struggling with using wildcards/generics. I'm trying to create a FileManager utility class that can accept custom Java Beans and write read/write the bean to a file. Just to give an example imagine I have an interface called Data that is…
Alex Cauthen
  • 497
  • 5
  • 12
  • 32
0
votes
2 answers

Java - Calling method from child of abstract class

Given the following abstract class: public abstract class BaseVersionResponse { public abstract void populate(T versionVO); } and the following child class: public class VersionResponseV1 extends…
risingTide
  • 1,754
  • 7
  • 31
  • 60
0
votes
3 answers

Meaning of the different parameter types in java java.util.List interface

What is the difference between public List myList; and public List myList; I know the latter will store a List of myList objects (Strings) but am not sure what the first does.My guess is it will take object of any type.But is this safe?t
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
-1
votes
1 answer

How to add some value in generic wildcard map

I have gone through couple of related questions but those are not help me out. Actually, I have a method whose return type is Map and I want to do unit test using Mockito. Sample Code is: Map resultMap =…
-1
votes
1 answer

What is difference between the usage of Unbounded wild card and a Generic type in Java?

Why Java has provided Unbounded wild card if we can solve the problem using Generic type . For example class UnBoundedUsage { //Method 1 public static void unboundedMethod(List list) { for(Object obj : list) { …
MishraJi
  • 304
  • 2
  • 6
  • 18
-1
votes
1 answer

Attempt #3: Cannot form expression of required type with two question marks in Java

My previous SSCCE was incorrect itself. I tried to write one more, but it was incorrect too. So, for now I don't understand the problem and hence can't write pure Java example and therefore am posting example with library classes: import…
Dims
  • 47,675
  • 117
  • 331
  • 600
-1
votes
1 answer

Casting bounded wildcard to unbounded wildcard within a generic type is an error (X> to X>

Consider this sample: private void m(Class k, Set> sk) { Class ku = k; Set> sku = sk; // <-- Type mismatch: cannot convert from // Set> to…
davidbak
  • 5,775
  • 3
  • 34
  • 50
1 2 3
4