7 Replies - 3338 Views - Last Post: 18 August 2010 - 06:25 PM Rate Topic: -----

#1 mk.ranjan  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -5
  • View blog
  • Posts: 26
  • Joined: 28-June 07

Generics benefits

Posted 18 August 2010 - 04:35 AM

What could be the benefits of using generics while coding
Is This A Good Question/Topic? 0
  • +

Replies To: Generics benefits

#2 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: Generics benefits

Posted 18 August 2010 - 04:43 AM

what do you mean by generics? generics of what?
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Generics benefits

Posted 18 August 2010 - 05:44 AM

View PostZekorov, 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
Was This Post Helpful? 2
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

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.
Was This Post Helpful? 2
  • +
  • -

#5 mk.ranjan  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -5
  • View blog
  • Posts: 26
  • Joined: 28-June 07

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
Was This Post Helpful? -2
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10372
  • View blog
  • Posts: 38,411
  • Joined: 27-December 08

Re: Generics benefits

Posted 18 August 2010 - 07:00 AM

View Postxclite, 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Generics benefits

Posted 18 August 2010 - 04:24 PM

Smells the homework 50 miles around.

mk.ranjan if you didn't already have a -2 for just a few posts I would have give you another -1 here.

Please respect this forum rules that are showed to you every time you post a new topic.
Was This Post Helpful? 0
  • +
  • -

#8 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

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. :D Learning new stuff everyday hehe
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1