Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Junky

Thread: Using Generics

  1. #1
    Join Date
    Apr 2010
    Location
    Warakapola, Sri Lanka
    Posts
    15
    Rep Power
    0

    Default Using Generics

    Hi everybody,
    I have some questions about generics. Those are,
    When I use generics?
    What are the issues if I don't use Generics?
    Should I use generics in applications?

    Thanks you.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Generics are used to enforce type safety. If you create a non-generic List you can add all sorts of objects to it. Whereas if you add the generic type to the List when you create it the only type of objects you can add to the List are those of that type (or subclassses).

    You can choose to ignore Generics if you like. When you compile your code you will be given a warning, however the code will still compile (provided there are no other errors) and run.
    darshanaprasad likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    @OP: I removed your other identical thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Good lord...I only just noticed you're a moderator!
    What fool thought that was a good idea?


  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Good lord...I only just noticed you're a moderator!
    What fool thought that was a good idea?

    They forced me to do it; I demanded that I'm the official operator of the rotating knives for the spam-o-matic machine ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I thought you are the rotating knives, what with your cutting wit and all that.

    db <-- b stands for blunt
    Last edited by DarrylBurke; 06-09-2011 at 12:10 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    I thought you are the rotating knives, what with your cutting with and all that.

    db <-- b stands for blunt
    Nonono, I'm just the humble operator and take care of the spectacle when we put the spammer in the spam-o-matic and close the transparent lid; oh what fun we all have watching the tumbling bloody carcass over and over again. imho, we all should take turns cleaning the knives afterwards ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    "JosAH, the invisible moderator"

    @op: junky gave a good explanation of generics, if you want to read some more, check out the generics tutorials.

    Lesson: Generics (The Java™ Tutorials > Learning the Java Language)

    There are plenty of subtle things to learn about generics. For example, they are implemented through erasure to allow backwards comparability with older code. Erasure means that the type is unknown at runtime. Generics allow incorrect code to fail at compile time, which is a good things. The earlier code fails, the easier it is to fix errors. The compiler basically casts the returned object to the correct type.

    Java Code:
    List<Integer> ex = new ArrayList<Integer>();
    ex.add(5); //type is an integer or an error is caused
    ex.remove(0); //the return type is cast to Integer
    Without generics it can be thought of like this
    Java Code:
    List ex = new ArrayList();
    ex.add(5) //stored as an Object
    Integer a = (Integer) ex.remove(0); //cast back to integer
    A problem with not using generics is, what if you make a list that should only contain integers, when extracted or used the items are cast to Integer, well what if someone intentionally, or unwittingly puts a non integer in the list? Sometime after compile time you will get an error for trying to cast type E to Integer. The time the item is added can be far removed from when the cast causes a problem, which can make the code harder to debug.
    Last edited by sunde887; 06-09-2011 at 11:38 PM.

Similar Threads

  1. Help with generics
    By shai in forum New To Java
    Replies: 0
    Last Post: 08-12-2010, 07:07 AM
  2. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 10:12 PM
  3. Generics
    By bschmitt78 in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2010, 02:21 AM
  4. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  5. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •