Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question(s) on Generics/Templates

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default 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.

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default 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.
    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>();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.
    Last edited by copeg; January 7th, 2012 at 10:48 AM.

Similar Threads

  1. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  2. Generics warnings
    By silverbird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2011, 08:55 PM
  3. Generics
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: May 19th, 2011, 06:44 PM
  4. Arrays and Generics
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2011, 09:44 PM
  5. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM