0

As far as I know, .add method for ArrayList cannot add String elements to integer ArrayList. For instance,

import java.util.ArrayList;


public static void main (String...args){
    ArrayList<Integer>lst = new ArrayList<Integer>();
    lst.add("ab");
    System.out.println(lst.get(0));
    }
}

fails to compile.

but if you you a separate method,

import java.util.ArrayList;


public class Test {
    public static void addToList(ArrayList a){
    a.add("abcd");
    }

public static void main (String...args){

    ArrayList<Integer>lst = new ArrayList<Integer>();

    addToList(lst);
    System.out.println(lst.get(0));
    }
}

This will print "abcd" without issue.

Why is it that you can actually add a String to an integer ArrayList by using a method?

4
  • maybe because you haven't specified the type of that array list Commented Mar 28, 2017 at 3:32
  • 1
    addToList(ArrayList a) is a raw type. Don't use raw types. Commented Mar 28, 2017 at 3:32
  • 1
    My understanding is that the reason Java generics were designed to let you do this was so they didn't break existing programs in pre-generic Java, when everything had to be cast. Lesson - don't use raw types (did anyone say that already?) Commented Mar 28, 2017 at 3:34
  • @DavidWallace That is part one. Part two was to make generics require no run time overhead. But yes, since we both forgot to mention it: Don't use raw types. Commented Mar 28, 2017 at 3:37

4 Answers 4

2

In your second example, you have dropped the generic type when passing the ArrayList to the method.

Since Java does not enforce generics at runtime, you can pass whatever you want since it will just be used as an Object.

Sign up to request clarification or add additional context in comments.

Comments

0

Why is it that you can actually add a String to an integer ArrayList by using a method?

It is because at run time, there will be no type. Type erasing will happen and your array list become generic.

In your first case, the compiler puts the check in.

Why compiler failed in second case, its because you had an arraylist with out a type

 public static void addToList(**ArrayList** a){
    a.add("abcd");
    }

Hence you can add anything to it.

Comments

0

Welcome to the joys of java generics. The answer to this is because you didn't just separate the method, you also declared the method with the raw type instead of ArrayList. If you had included that in your function name, it wouldn't have compiled as the compiler would be able to detect the mismatched types.

The reason for this is called type erasure, and what happens is java byte code doesn't actually contain all of the information you supply to the compiler. The byte code just contains the raw type ArrayList, and the parameterization which results in compiler errors is a developer convenience to help prevent type casting related bugs. So you're actually free to put any type of object within a Collection if you use the raw types references. Don't do this however, as you're just asking for trouble and sticking with the compiler assist is best practice for obvious reasons.

Comments

0

In you second sample, a raw type variable a = new ArrayList<Integet>, just as subClass extends superclass, which can invoke method belong to superclass and don't need to check type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.