0

I am practicing Java Generics trying to understand raw types vs generic types.

I have created a raw list and added a String element. I have also created an Integer list added an Integer to it. Now I have assigned the raw list to the Integer list (integerList = rawList; - It just throws a warning message). Now in the Integer list, I have a String element and able to print it. I am not able to add new String elements to Integer list as compiler checks during compilation.

But by assigning a raw list which contains a String (or any other type) we are able to store a String (or any other type) in an Integer list. Don't you think it's a bug in Java?

I tried searching google and stackoverflow to find out an answer for this but didn't get any solution for this.

package generics;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GenericBox<T> {

    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>(Arrays.asList(1,2,3,4));

        List rawList = new ArrayList();
        rawList.add("Subbu");

        integerList = rawList;
        integerList.add(5);

        System.out.println(integerList);
        System.out.println(integerList.get(0));
        System.out.println(integerList.get(1));

    }
}
Result:
[Subbu, 5]
Subbu
5

I don't expect String to be stored in an Integer list and feel it like a bug in java. What do you think?

Thank you, Subbu.

Subbu
  • 217
  • 2
  • 11
  • I'd read [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) – GBlodgett Sep 05 '19 at 21:49
  • 1
    No, you lied to the language to get around the type checker. It did give you a warning, as you noted, telling you that what you were doing was wrong. It wouldn't necessarily always be possible for it to be able to tell at compile time what elements the raw list holds, so it can't issue an error saying that the types don't match. It's trusting you (but giving a warning for good measure). – Carcigenicate Sep 05 '19 at 21:50
  • I'm not so sure that was a good closure. They don't seem to be asking what a raw type is. – Carcigenicate Sep 05 '19 at 21:51
  • @Andy Turner, can you give me the post links where this question is already asked? – Subbu Sep 05 '19 at 21:53
  • "I don't expect String to be stored in an Integer list" There's no such thing as an "Integer list": there are just `List`s, and you ask the compiler to stop you putting things that aren't `Integer`s into it. But if you have broken the type checking by using raw types, and there's already a `String` in the list - there's nothing to prevent that at runtime, because generics are (nearly) purely a compile time concept. – Andy Turner Sep 05 '19 at 21:53
  • @Subbu the yellow box at the top of the question. – Andy Turner Sep 05 '19 at 21:54
  • @Andy Turner, Thank you Andy, I will look into the post. But I think your comment that "There's no such thing as Integer List....." has already answered my question. – Subbu Sep 05 '19 at 21:57

0 Answers0