Java - What is the output: null value and generic type?

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    Wrapper<?> stringWrapper = new Wrapper<String>("Hello");
    Object str = stringWrapper.get();
    System.out.println(str);
    stringWrapper.set(null);
    str = stringWrapper.get();
    System.out.println(str);
    
    
  }
  public static void printDetails(Wrapper<?> wrapper){
    System.out.println(wrapper);
  }
}

class Wrapper<T> {
  private T ref;

  public Wrapper(T ref) {
    this.ref = ref;
  }

  public T get() {
    return ref;
  }

  public void set(T a) {
    this.ref = a;
  }
}


Click to view the answer

Hello
null

Note

The set(T a) method accepts the generic type argument.

A null is assignment-compatible to any reference type in Java.

No matter what type T would be in the set(T a) method, a null can always be safe to use.