Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
praveen kumaar wrote:in the first case you have used a generic type declaration for the refrence variable but not for the object instantiated.so after the type erasure performed by the compiler,it founds the upper bound for T is Object and Integer is a object(by inheritence).so it doesnot complain...
in the second case you have declared the generic type during the instantiation so the upper bound sets to String and Integer is not a String so it complains.
hope it helps.
regards
praveen.
Daniel Cox wrote:In case 1 (new Gen(1)), since you have not supplied a type argument, you get an unchecked compiler warning because the compiler does not have enough type information to ensure type safety i.e. the compiler does not know the type of argument that must be passed to the constructor in order to ensure type safety.
In case 2 (new Gen<String>(1)), since you have supplied a type argument, you do not get an unchecked compiler warning because the compiler has enough type information to ensure type safety. In order to ensure type safety, the compiler expects that you pass a String to the constructor but you have passed an incompatible type and so you get an incompatible types compiler error.
Daniel Coxb wrote: The compiler will not guarantee type safety if you mix legacy code with generic code.
Puspender Tanwar wrote:...since we are have single T type parameter declared at class name syntax, and we are using the same T in the constructor(No independent type param for constructor)...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
A raw type is defined to be one of:
.The reference type that is formed by taking the name of a generic type declaration without an accompanying type argument list.
.An array type whose element type is a raw type.
.A non-static member type of a raw type R that is not inherited from a superclass or superinterface of R.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Stephan van Hulst wrote:Type erasure happens during compilation, meaning any code you write that references a parameterized type will be checked before that point.
Let's take a list as an example:
praveen wrote:
for how the compiler gives a type safety is if the parameter type is mentioned then compiler will check for the argument type before being used with the declared parameter type
if it founds a difference then you will get a compile time error.
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|