Generics data type is not working to char data type of Array | Core Java Forum
V
Vikrant Posted on 21/07/2021

Hi Yogesh,

I have taken a char [] array and the passed the char[] array in generics methods so its giving me compile time error. Then I have changed the char[] data type array in Character[] class array (wrapper class of char) then its working. why it does not works with char data type array?


Y
Yogesh Chawla Replied on 21/07/2021

This is because char is a primitive type and T represents an object. If you use Character[] instead of char[], it should work fine. This copies all of the characters from the char array into the Character array and then passes it to the method.

Do this:

char[] chars = {'a','b','c'};
		Character[] charArr = new Character[chars.length];
		for (int i = 0; i < chars.length; i++)
			charArr[i] = chars[i];

and then pass charArr to generics method.