What will be the output of the following code?

public class Test<M> {

    <M> Class getArgumentClass(M m) {

        return m.getClass();
    }
    
    public static void main(String[] args) {

        Test test = new Test();
        
        Double d = new Double(12);

        Class c1 = test.<Double>getArgumentClass(d);
        Class c2 = test.<Number>getArgumentClass(d);
        Class c3 = test.getArgumentClass(d);
        
        System.out.println(c1 == c2);
        System.out.println(c1 == c3);
        System.out.println(c2 == c3);
    }
}
Explanation
Although the code will fail to compile in Java 6 with the following error: "Unresolved compilation problems: The method getArgumentClass(Object) of raw type Test is no longer generic; it cannot be parameterized with arguments" - in Java 7 this issue has been fixed. The compiler will show the following warning message: "Type safety: The method getArgumentClass(Object) belongs to the raw type Test. References to generic type Test<m> should be parameterized", but the code will compile and the output will be
true
true
true

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback