0

I have seen plenty of times how when declaring a data structure only round brackets are used while doing the dynamic allocation and many times, we have to include the wrapper class's name inside the Angle brackets? What's the major difference in these two declarations?

E.g.

HashSet<String> h = new HashSet();

and

LinkedList<String> l = new LinkedList<String>();
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • The difference is, for example, that the first way won't work. I suppose you meant `new HashSet<>();`? – Federico klez Culloca Jul 19 '23 at 10:55
  • 2
    The diamond operator `<>` was added in Java 7 and you should [program to an interface](https://stackoverflow.com/q/383947/2970947). `Set h = new HashSet<>();` and `List l = new LinkedList<>();` – Elliott Frisch Jul 19 '23 at 11:01
  • 1
    BTW - `HashSet h = new HashSet();` will work, just with a compiler warning "unchecked conversion" – user16320675 Jul 19 '23 at 12:05

0 Answers0