Java 1.5 introduced this thing and its nice. Something that helps you to get rid of all ClassCastExceptions. Also it allows you to create better and elegant systems. I have just started off with this and liked it....The negative aspect of generics (if any) havent yet been spotted and i would really like ur comments if u know of anything.
Everything about genrics is here
Sunday, March 25, 2007
Subscribe to:
Post Comments (Atom)
7 comments:
According to my understanding "Generics" are the same thing as STL (static template library) which has been for long in C++. Now obviously it drastically improves type safety which is its beauty. Though I feel it takes away one of the most powerful feature and flexibility from collection framework containers i.e. we can no more store anything in the container.. and Irony is that they no more function as truly generic containers.
P.S: Please correct me if I am wrong as I am just an admirer of Java language and not a Java pro :)
Vikash,
I dont understand the loss of flexibility. I mean the type safety that gets introduced because of generics just issues a warning incase u skip it (unless Java makes it mandatory......which then spoils the whole purpose)
eg: If I create a HashMap i could still add anyhting to the HashMap.
Its only when i create HashMap(String,String)[that's HashMap with amgular brackets ,it doesnt allow me to use angular brackets while writing this comment :(] that i compulsorily need to add specific types into teh Hashmap. So i feel we just get extra functionalities and the basic ones remain same.
what say? and yup, i have heard its same as CPP's STL...though no first hands experience with it
You can get ClassCastExceptions while dealing with get/put methods of Java Collections, since you still do 'get' using an Object as key parameter. Dont know why that annoying legacy remains. Does anybody know that?
I did like to know how java's algorithms compare to STL algorithms, which is just a wonderful piece of library design
Anoop,
The ClassCastException that would be thrown would be the existing behaviour for Collections and not something introduced by generics.
so ideally there is no flexibility loss as many point out.
Agreed. Point was the collection interface should also have been generic-fied completely. I guess that is where generics are used the most. I have wasted so much time dealing with run-time exceptions - it would be great to have compile time errors flagged.
Generics were introduced in Java only after C# introduced them (well in the CLR), whereas Java's implementation is a compile-time impelementation. The JVM itself doesn't understand them.
On .Net you can dynamically provide the type that you want to use as well, answering a query above. :-)
(A true Microsoft person speaking)
Well, I dont know about Generics a lot but I notice that you also (like me) havent frequented Blogger a lot in the last 6 months. In case the comparison to me sounds weird ... hahahaha, let me take back my words ... I havent been on Blogger for more than 6 months I guess.
Cheers ... !!
Post a Comment