Wednesday, September 26, 2012

Java Generics

Sometimes JAVA is really bothersome. JAVA has generics for quite some times; okay generics is nice to have. You don't have to cast objects and you can efficiently work with typed objects. I just learned that JAVA does not support the array of generic objects! ooops! Thant's mean you cannot create an array of ArrayList<Double>. What you can do is create an array of  ArrayList<?> or  ArrayList  and later cast to   ArrayList<Double> whenever you try to access. What's the purpose of generics if I have to cast! Even worse if you do that without checking the type, JAVA is going to give you a warning! And apparently you can ignore the warning or you can check the type before casting, which means one more if statement (extra complexity)! This is not cheap when you are considering large number of items!

Interestingly, whatever I have just said is partially true. Actually you can create an array of ArrayList<Double>, but you have to initialize it with untyped ArrayList! Wow! and when you do that JAVA gonna give you an warning because of untyped ArrayList! So guess what, there is just no way to create an array of generics without warning. I don't know whats wrong with the generics array! Sometimes generics in JAVA seems half baked to me.

No comments:

Post a Comment

Please, no abusive word, no spam.