


This error tells me that the compiler is using the resulting type of the input argument expression The compiler sees a perfect match of the receiving variable ls and return type of the method.īut if you uncomment "Test 1." statements, you will get a compilation error in JDK 1.8.The compiler determines that the return type of the method should be List based on the return type definition.The compiler determines that the actual type of the type parameter, T, should be String based on the input object.The compiler looks at the type of the input object, s, and sees that it has a type of String.Here is my guesses on how the compiler did the type argument inference to determine the actual type of the type parameter, T,Īnd did the type checking on the statement: Looking at the statement where the generic method is invoked (List ls = Collections.singletonList(s) ), o.println(" First element of li2: "+e2) Ĭompile and run the example, you will get:Ĭlass name of ls: $SingletonList o.println(" Class name of li2: "+li2.getClass().getName()) Comipler warning - unchecked or unsafe operations up casting to the wildcard parameterized type

List ls1 = Collections.singletonList((Object) s) O.println(" Class name of ls: "+ls.getClass().getName()) O.println("Testing: List singletonList(T o)") Copyright (c) 2014,, All Rights Reserved. " List singletonList(T o)", defined in the Collections class. Here is my first tutorial example on testing generic method, In this section, I want to continue to test on type argument inference on generic method that uses type parameters Type argument inference tests are provided on the generic method, singletonList(), from the class. This section provides a tutorial example on generic methods that use parameterized types.
