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 3 of 3

Thread: Generics

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Generics

    Hi all.

    Managed to find a link which talks about type erasure. Or more specifically, it talks a bit about how you can avoid type erasure in generics. According to the link, a generic type can be known at runtime during some situations. Does anyone have a thought on this text? Does this mean that if you do something like this you are actually avoiding using type erasure?
    ArrayList<String> alist = new ArrayList<String>() {};
    Do not know how much this actually matters, I am just curious . The link:
    Using TypeTokens to retrieve generic parameters - JQuantLib

    Take care,
    Kerr.

    P.S.

    I confess I didnt read it very carefully, so if I have missed something or missunderstood it then that is why, lol.

    EDIT:

    Lol, just realized I forgot to give the thread a better title. Sorry about that.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Generics

    Type erasure isn't something you use, it's what the compiler does. The generics syntax is a compile-time way for the compiler to check that you're using the correct types. All (most of) that type information is removed during compilation so the compiled code doesn't have it and the Java runtime can't use it. Hence 'type erasure'. See Type Erasure.

    For technical reasons, the runtime needs some type information for anonymous classes, so some type information is retained when anonymous classes are compiled. This is accessible (through messy tricks like those in the article), but it really seems like cutting off your nose to spite your face - making the syntax a tiny bit simpler or avoiding a minor inconvenience by sacrificing simplicity, robustness and performance... YMMV.
    Last edited by dlorde; May 15th, 2011 at 01:09 PM.

  3. #3
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Generics

    I know what type erasure is. But I confess I got kind of confused by the article. Thought that if you could avoid making casts then it could improve performance, or something. Now that I think of it I realize it would just become a mess.
    Last edited by Kerr; May 19th, 2011 at 06:51 PM.

Similar Threads

  1. Arrays and Generics
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2011, 09:44 PM
  2. How to use Generics (1 generic anyway)
    By javapenguin in forum The Cafe
    Replies: 4
    Last Post: February 7th, 2011, 06:15 PM
  3. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM
  4. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  5. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM