Question(s) on Generics/Templates
Hi All,
Is there a area in programming where using Generics will greatly improve the programming duration or efficiency of the program as compared to using non parameterized types ?
Examples like DB programming ? Real time systems ? Multimedia processors.. Hope you get what I mean..
If any users with real life experience working with generics, could share their experiences.. that would be great too.
Re: Question(s) on Generics/Templates
I am thinking you can use Generics where for instance an array is just too inefficient. Consider using an Arraylist from the List. I am not quite clear on this but I think parametarized types are obviously there to improve a programmer's work.
Re: Question(s) on Generics/Templates
Generics rock! They cut out a lot of work and a lot of errors. By defining exactly what kind of objects a collection should hold you don't need to cast, check types or handle cases where strange things end up in the collection.
I very rarely write a program without them. Here is something I was working on about 2 minutes ago.
Code java:
StarSystem starSystem = new StarSystem();
LinkedList<Star> stars = starSystem.getStars();
LinkedList<Planet> planets = starSystem.getPlanets();
LinkedList<TextView> starView = new LinkedList<TextView>();
LinkedList<TextView> planetView = new LinkedList<TextView>();
Re: Question(s) on Generics/Templates
My idea of generics is that it allows the compiler to keep the programmer from making mistakes when coding with data types. The compiler can check that the data types are correctly used.
I don't know if there is any efficiency. Perhaps you could write two very small simple programs, one with and one without generics and look at the generated code and tell us which one generated fewer code bytes.
Re: Question(s) on Generics/Templates
It all boils down to errors encountered at compile time vs runtime. Generics provide compile time checking for data types, providing compile time errors when something does not match. Without them, one must rely on casting an Object to a required type. These casts can get ugly, but more importantly when done incorrectly result in ClassCastException - a runtime problem that can often be difficult to debug in larger applications.