For example,
ArrayList al = new ArrayList();
al.add(Object o);
The above statements do not use generic and is quite valid. However,a programmer might be able to cause an error if the objects that he adds are not the same type. If at first an integer is added and then a string is added later on, the compiler would not be able to detect this and a runtime error will occur.
Using a generic,
ArrayList al = new ArrayList();
will ensure that every time the method al.add() is called, the parameters added has to be a String.
No comments:
Post a Comment