What could be the benefits of using generics while coding
Generics benefits
Page 1 of 17 Replies - 5307 Views - Last Post: 18 August 2010 - 06:25 PM
Replies To: Generics benefits
#2
Re: Generics benefits
Posted 18 August 2010 - 04:43 AM
what do you mean by generics? generics of what?
#3
Re: Generics benefits
Posted 18 August 2010 - 05:44 AM
Zekorov, on 18 August 2010 - 06:43 AM, said:
what do you mean by generics? generics of what?
He means parameterized classes I assume. They're similar to templates in C++ and are a form of metaprogramming.
mk.ranjan the main benefit of generics is that they let you create data structures that can hold any type of data without having to resort to dealing with raw Objects.
ArrayList<Integer> foo = new ArrayList<Integer>();
http://en.wikipedia....enerics_in_Java
#4
Re: Generics benefits
Posted 18 August 2010 - 05:56 AM
To extract the main advantage from Tom9729's post, generics allow greater type safety and more specificity. You don't have to cast, you're allowed to only accept objects who conform to a certain interface or extend a certain class, and type checking is done at compile-time rather than run-time.
#5
Re: Generics benefits
Posted 18 August 2010 - 06:36 AM
I beleive i have posted my question under java forum, so expecting Java Generics related answers only
#6
Re: Generics benefits
Posted 18 August 2010 - 07:00 AM
xclite, on 18 August 2010 - 08:56 AM, said:
To extract the main advantage from Tom9729's post, generics allow greater type safety and more specificity. You don't have to cast, you're allowed to only accept objects who conform to a certain interface or extend a certain class, and type checking is done at compile-time rather than run-time.
I believe xclite hit it right on. In the days before generics, you would have to do things like extend ArrayList if you and overload its methods to make it specific for Integers only. With Generics, ArrayList can handle any reference data type without subclassing as we did pre-generics. Also with Generics, you can place restrictions on which types are eligible for the class to work with. Things like:
<T extends Comparable<? super T>>
Enforce that T implements Comparable, and that the compareTo() method is compatible with T and its subclasses. You can do more with generics, but that is the gist.
#8
Re: Generics benefits
Posted 18 August 2010 - 06:25 PM
Hey thanks tom9729 for letting me know what he meant by Generics.... i had no idea what they were.
Learning new stuff everyday hehe
Page 1 of 1

New Topic/Question
Reply



MultiQuote








|