I started to learn generics today, but this is somelike weird for me:
I have a generic method:
public<T> HashMap<String, T> getAllEntitySameType(T type) {
System.out.println(type.getClass());
HashMap<String, T> result = null;
if(type instanceof Project)
{
System.out.println(type.toString());
System.out.println("Yes, instance of Project;");
}
if(type instanceof String)
{
System.out.println(type.toString());
System.out.println("Yes, instance of String;");
}
this.getProjects();
return result;
}
And i can easily determinate the class of the T type
Project<Double> project = new Project<Double>();
company2.getAllEntitySameType(project);
company2.getAllEntitySameType("TestString");
The output will be:
class Project
Yes, instance of Project;
class java.lang.String
TestString
Yes, instance of String;
I thought in generics we can't use instance of. Something is not complete in my knowledge. Thanks...
type instanceof T;)System.out.println(type);so you don't even know if it isnullor not.