简体   繁体   中英

Generic method invokation with no parameter

If i have a generic method

public <T> ArrayList<T> test(){ return new ArrayList<T>(); }

And invoke him without explicit type parameter then will be returned raw type ArrayList , thus if we invoked generic method without type parameter we are invoked just corresponding non-generic method. Is it true?

No, if you are allowed to invoke the method without an explicit type parameter, that means that the compiler was able to infer the appropriate type for you. That means an ArrayList<WhateverInferredType> will be returned.

No. This method is always called with a type paramater, and the ArrayList will always have a type paramter. The parameter is determined via type inference . In the most basic case (ie just calling the method), the type parameter will be Object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM