-4

Is there any difference between these 3 lines?

List list2 = new ArrayList<String>();
List  list2 = new ArrayList<>();
List  list2 = new ArrayList();

As I understand, all information about generics is erased in runtime. Consequently, only type of veriable is important. So the above lines of code mean the same thing to me. If I am wrong can anybody give me some exapmle that shows the difference? p.s. sorry for my english

Viacheslav On
  • 73
  • 1
  • 7
  • I do not believe your second example is valid, as the `<>` operator will not have any information. It could be `List list2 = new ArrayList<>();` – KevinO Jun 14 '17 at 13:21
  • 5
    Generics are erased at runtime, but they exist at compilation time and let compiler detect many potential problems. In this case what you are using is raw type `List list2` which can accept any list, but because of that it is prone to many problems. More info: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Pshemo Jun 14 '17 at 13:23
  • sorry, second line is wrong, my mistake. But anyway, I know about raw types, I am interested "in right part of expression". What the difference: new ArrayList(); new ArrayList (); I think the both mean the same – Viacheslav On Jun 14 '17 at 13:28
  • Possible duplicate of [What is diamond Operator in java?](https://stackoverflow.com/questions/16352816/what-is-diamond-operator-in-java) – OH GOD SPIDERS Jun 14 '17 at 13:30
  • 3
    @ViacheslavOn "What the difference: new ArrayList(); new ArrayList (); " <- It is obvious you **don't** "know about raw types" otherwise you wouldn't ask that question. Because the difference between those two is that one is a raw type while the other isn't. – OH GOD SPIDERS Jun 14 '17 at 13:31
  • btw, second line is valid since Java 7 – Viacheslav On Jun 14 '17 at 13:41
  • @OH GOD SPIDERS what dont I understand? Raw types return Objects. If I use List list (raw type) any ArrayList will return Objects, wether it`s "new ArrayList();" or just "new ArrayList();" The same – Viacheslav On Jun 14 '17 at 13:46
  • See: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Jesper Jun 14 '17 at 13:50
  • why don`t you understand that it doesn`t metter is there <> in right part of expression or not? You can`t give some example, because there is no such examples – Viacheslav On Jun 14 '17 at 14:41
  • raw types refers just to variables but not to certain instance – Viacheslav On Jun 14 '17 at 14:43

1 Answers1

0

While it is true that generic data is erased at compile-time, that doesn't mean they are totally useless. The only line that would compile in your example is the last one, but you would get a warning that you are using RawTypes. A RawType is a generic class that does not have a generic object. The first two lines are only half-built.

The reason you pretty much have to use generics is that the list is type-safe. If you use generics, you could use something like this:

List<String> list = new ArrayList<>();
String out = list.get(0);

If you use raw types, you would have to do this:

List list = new ArrayList();
String out = (String) list.get(0);

This may seem OK, but what if you add a value to the list that is not a String? The program crashes. For example:

List unsafe = new ArrayList();
List<String> safe = new ArrayList<>();
unsafe.add("hi");
unsafe.add(new Tree());
safe.add("hi");
safe.add(new Tree());    // This line would throw an exception.
String out = (String) unsafe.get(0);
String out1 = safe.get(0);
String out2 = (String) unsafe.get(1);    // This line would throw an exception.

If you still don't quite see why to use generic types, the final nail in the coffin for raw types is this: If my (String) unsafe.get(1) is in a separate class, on the hundredth line, I know that that line is the problematic line. I, however, don't know where the non-string object is being added, only where it's being accessed.

If you use generic types, you know exactly where the problematic addition is made, and you can prevent it.

CJ Burkey
  • 385
  • 1
  • 14
  • sorry, but all lines definitely compile and there is no any warnings. And I know all that you have wrote, but it is not answer. – Viacheslav On Jun 14 '17 at 15:03
  • @ViacheslavOn If you don't think anything I wrote is the answer, then you don't seem to understand generic types, this video explains it better: https://www.youtube.com/watch?v=1tKmzQh8g5E – CJ Burkey Jun 14 '17 at 15:08
  • sorry, but all lines definitely compile and there is no any warnings. And I know all that you have wrote, but it is not answer. "A RawType is a generic class that does not have a generic object." - It is clear. But wheter class raw or not depends only on wheter reference is parametrized. And wheter object is parametrized or not It doesnt metter. – Viacheslav On Jun 14 '17 at 15:10
  • Watch the video, he provides more coherent and better-explained examples and reasons than I did. Even though the generic types are removed at compile-time, they are still extremely useful. And if for no other reason, you should use them because `RawTypes` only exist to be backwards compatible. – CJ Burkey Jun 14 '17 at 15:13
  • Why don`t I understand? You sad that 2 out of 3 lines wouldn`t complile. But all 3 would. So I guess we should start from this – Viacheslav On Jun 14 '17 at 15:14
  • 1
    No, that was an incorrect statement on my part. All generic lists can be cast to raw-lists. You could not, for example, have this: `List list = new ArrayList();` while you can have: `List list = new ArrayList();` – CJ Burkey Jun 14 '17 at 15:15
  • To *directly* answer your question, *technically* the lines are different, but can be treated as the same. They are all `List`, so you could do the same things to all of them. None of them are generic lists however, not even the first one. They are all RawTypes – CJ Burkey Jun 14 '17 at 15:18
  • It is definitely answer that I was looking for.Because it is really doesn`t metter how I create ArrayList if my List is raw type. I was thinking that maybe I don`t understand something. And some people says that I don`t. But no one could say why – Viacheslav On Jun 14 '17 at 15:23