Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:32:04+00:00 2026-05-26T17:32:04+00:00

As far as i know, the generics are only useful at compilation time. Thus

  • 0

As far as i know, the generics are only useful at compilation time.

Thus it is possible to declare:

private Set set = new HashSet<String>();

And then, in this string hashset, to add dogs or anything to that set, without any problem since there is not any check at runtime like for arrays (ArrayStoreException…) (but you may have problems like a classcast when you use that set…

So what i want to say is, we usually instantiate generic collections that way:

Set<String> set = new HashSet<String>();

My question is, why do we put the type of the HashSet, since only the type of the reference of the variable is really important (i think).

I mean, we could simply write:

Set<String> set = new HashSet();

And it would work exactly the same right?
So why do we usually write the type during the instantiation? (it’s not mandatory)


Edit

I know about the “diamond operator” for type inference but why do we even need it!!! since type inference is already working!

The following code :

            Set<String> set = new HashSet();
            set.add("Test add a string 1");
            set.add("Test add a string 2");
            for ( String s : set ) {
                    System.out.println(s);
            }

Produces the output:

Test add a string 1
Test add a string 2

Test it yourself
http://ideone.com/vuiCE

So now you talk about type inference as a Java7 feature, but it is already working for me…

With java7 i will have to replace my

            Set<String> set = new HashSet();

By

            Set<String> set = new HashSet<>();

It’s still 2 extra characters for doing exactly the same thing no? (Unless generics are not only compile time with Java7? I don’t know)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:32:04+00:00Added an answer on May 26, 2026 at 5:32 pm

    Although Java currently erases generic type info, they have been extremely careful to design the language and libraries so that it is possible in future to add back full generic type info (so called reification). Any correctly written generic code (i.e. without any javac warning) will survive such language change. In your example, the diamond version is future proof; the raw version is not – it still must compile of course, but it will throw type cast exception at runtime.

    Yet, this future-compatibility is a completely delusional. Erasure will stay with us forever. If full reification is introduced, it is safe to say almost all non trivial generic code will break, because everybody is writing incorrect generic code.

    It is not our fault. Java forces us to. It gives us the bullshit erasure, and we have no choice but to hack around it to implement stuff. Our code is full of dependencies on detail knowledge of erasure.

    JDK is the most guilty one. JDK APIs are carefully designed to survive reification; however the implementations depend on erasure. If Java adds reification, these JDK codes must be rewritten. Java team will be extremely hypocritical if they blame us for writing incorrect code while they must do the same thing themselves.

    Given the amount of code that cannot survive reification without rewrite, we can safely say Java is never going to add reification, as long as it keeps the commitment on backward compatibility.

    In that case, yeah, whatever. Nothing really terrible about Set<String> set = new HashSet(). Personally I always add <> to avoid the warning. (Actually whenever possible I’ll try to make my code reification-proof, which is, as I said, a delusional act)

    In your example, inference is trivial. There can be more complex case where <> inference is not so trivial, its type checking can be helpful, which the raw version lacks.

    class Foo<T>
    {
        Foo(Class<T> clazz){}
    }
    
    Foo<String> foo1 = new Foo(Integer.class);    // compiles (wrongly) 
    
    Foo<String> foo2 = new Foo<>(Integer.class);  // does not compile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as i know it is not possible to do the following in
As far as I know, only the convenience methods return created objects with an
so far i only know to use Ctrl-Shift-A to do SVN in TextMate. is
As far as know, I must be careful with PHP, and I think Javascript.
As far as I know, foreign keys (FK) are used to aid the programmer
As far as I know, Flash has to pass info off to another external
As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
As far as I know (not much I'll admit), the currently popular programming paradigms
As far as I know, there's no way to use {% include %} within
As far as i know, there is no direct equivalent in C#. My current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.