Java - What is the output: Unbounded Wildcards?

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    Wrapper<Object> objectWrapper = new Wrapper<Object>(new Object());
    printDetails(objectWrapper); 

    Wrapper<String> stringWrapper = new Wrapper<?>("Hello");
    printDetails(stringWrapper); 

  }
  public static void printDetails(Wrapper<?> wrapper){
    System.out.println(wrapper);
  }
}

class Wrapper<T> {
  public Wrapper(T ref) {}
}


Click to view the answer

// Cannot use  with new operator. It is a compile-time error.
new Wrapper("Hello");

Note

You cannot use <?> to create an object of its unknown type.

error: unexpected type
                new Wrapper<?>("Hello");
                           ^
  required: class or interface without bounds
  found:    ?
1 error

Here is the fix

Wrapper<?> unknownWrapper = new Wrapper<String>("Hello");