java - How can a parameter in a Generic method be assigned to an Integer and a Character class at the same time? -


why code isn't showing compilation error?

public class generic {     public static void main(string[] args)     {         character[] arr3={'a','b','c','d','e','f','g'};         integer a=97;         system.out.println(non_genre.genmethod(a,arr3));     } }  class non_genre {     static<t> boolean genmethod(t x,t[] y)     {         int flag=0;         for(t r:y)         {             if(r==x)                 flag++;         }         if(flag==0)             return false;         return true;     } } 

if write normal code this(shown below)

public class hello {     public static void main(string[] args)     {         character arr=65;         integer a='a';         if(arr==a)  //compilation error,shows incompatible types integer , character             system.out.println("true");     } }    

then why above above running fine,how can t of integer class , array of t of character class @ same time,and if running why not printing true,ascii vaue of 'a' 97,so should print true.

because compiler infers object type argument invocation of

non_genre.genmethod(a, arr3) 

within body of method

static <t> boolean genmethod(t x, t[] y) { 

your type parameter t unbounded, , can seen object.

since x , elements of y of same type (t), can compared fine.

if (r == x) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -