Suppose I have a generic Parcel class and a generic delivery method as shown in the code below. The method prints and returns x, which is assigned a parameter of type Integer inside the method.
public class Parcel<T> { public <X> X deliver(){ X x = (X) new Integer(100); System.out.println(x); return x; } }
Inside the main, I call the deliver method by passing the Parcel parameter. However, it still prints 100.
public static void main(String args[]) { Parcel<String> parcel = new Parcel<>(); System.out.println(parcel.<Parcel> deliver()); }
This means that an argument of type Parcel passed in the print line does not play any role, and I expected an exception here. How it works?
Nanev source share