3 ms·
The author states: " The JVM is very powerful, but it has its own potential issues (type erasure and garbage collection come to mind)." Can someone explain wh
by bogomipz 11y ago
The author states:
" The JVM is very powerful, but it has its own potential issues (type erasure and garbage collection come to mind)."
Can someone explain why type erasures would be an issue? GC is a loaded subject I know but don't type erasures allow Java to have generics? What is the negative here?
- iamcreasy 11y agoJava 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 11y agoHow are true generics implemented then in other compiled languages?
- zastrowm 11y 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 11y agoI see, thanks!
- bogomipz 11y agoHow are true generics implemented then in other compiled languages?
- eva1984 11y agoType erasure prevents runtime access to type information. If type is preserved, in certain scenario it could be handy, like dynamically checking whether certain class conforms to generic type(better instanceof operator). One example is the Gson library, looks at what twisted hack it uses to preserve the generics information at runtime(hints: TypeToken class).