Search code examples
javagenericsgeneric-method

Assigning List of Integer Into a List of String


I was learning Generics in Java and I came close to a very interesting piece of code. I know in Java it is illegal to add list of one type to another.

List<Integer> integerList = new ArrayList<Integer>();
List<String> stringList=integerList;

So in the second line I get a compile time error.
But if I create a generic method inside a class like this,

class  GenericClass <E>{
    void genericFunction(List<String> stringList) {
        stringList.add("foo");
    }
    // some other code
}

And in the main class call the method with list of Integer I am not getting any error.

public class Main {
  public static void main(String args[]) {

     GenericClass genericClass=new GenericClass();
     List<Integer> integerList= new ArrayList<Integer>();
     integerList.add(100);
     genericClass.genericFunction(integerList);
     System.out.println(integerList.get(0));
     System.out.println(integerList.get(1));
  }
}

Output
100
foo

Why I am not getting any error?


Solution

  • You have mixed Generic with raw type. It will compile fine but at run-time it might fail because Generic information is lost at run-time.

    Generic should be used to track such bugs at compile-time.

    It's better explained at What is a raw type and why shouldn't we use it? in detail.


    Warning: Type safety: The method genericFunction(List) belongs to the raw type GenericClass. References to generic type GenericClass<E> should be parameterized.

    If you have two methods with same name with different Generic type of List then it results into compile time error. The compiler is unable to resolve Generic type in case of method arguments that can be proved by below sample code.

    Sample code: (compiler error - not a valid overloaded method)

    void genericFunction(List<String> stringList){...}
    void genericFunction(List<Integer> stringList){...}
    

    Make some changes and try it again:

    class  GenericClass <E>{
        void genericFunction(List<E> stringList) {
            ...
        }
        // some other code
    }
    
    ...
    
    GenericClass<String> genericClass=new GenericClass<String>(); // Genreric object
    List<Integer> integerList= new ArrayList<Integer>();
    integerList.add(100);
    genericClass.genericFunction(integerList); // compile time error
    

    Create methods in such a way

    class GenericClass<E> {
        private List<E> list = new ArrayList<E>();
    
        public void addAll(List<E> newList) {
            list.addAll(newList);
        }
    
        public void add(E e) {
            list.add(e);
        }
    
        public E get(int index) {
            return list.get(index);
        }
        // some other code
    }