I need to know what is the difference between java Generic type and Object class. Here are two codes and both works fine, I just need to know what is the difference between them :
Program 1 using Object Class: package a;
public class Generic {
private Object[] datastore;
private int size;
private int pos;
public Generic(int numEl) {
size = numEl;
pos = 0;
datastore = new Object[size];
}
public void add(Object a){
datastore[pos] = a;
pos++;
}
public String toString(){
String elements ="";
for (int i=0; i<pos; i++) {
elements += datastore[i] + " ";
}
return elements;
}
}
Program 2 using Generic Type: package a;
public class Generic<T> {
private T[] datastore;
private int size;
private int pos;
public Generic(int numEl){
size = numEl;
pos = 0;
datastore = (T[]) new Object[size];
}
public void add(T a) {
datastore[pos] = a;
pos++;
}
public String toString(){
String elements ="";
for (int i=0; i<pos; i++) {
elements += datastore[i] + " ";
}
return elements;
}
}