4 ms·
Java doesn't have true generics. It uses erasures to emulate this feature. During compilation, template variables are removed and replaced with specific type. I
by iamcreasy 10y ago
Java doesn't have true generics. It uses erasures to emulate this feature. During compilation, template variables are removed and replaced with specific type. I think that's why it's an issue.
- bogomipz 10y agoHow are true generics implemented then in other compiled languages?
- zastrowm 10y agoIn the CLR/C# for example, List<int> is a distinct type from List<bool>. At runtime, when you create List<int>, the CLR will create a new type based on List<T> that is specialized for int. For List<bool>, a new type based on List<T> is specialized for bool. Then at runtime, you can test variable (if (untypedObj is List<int>)) because there are distinct types for each closed generic type. In java, the compiler would instead (I'm simplifying here), replace List<int> and List<bool> with List<object>, and at runtime, you wouldn't be able to tell a List<int> from List<bool> because their types were "erased" and became List<object>.
- bogomipz 10y agoI see, thanks!
- bogomipz 10y agoHow are true generics implemented then in other compiled languages?