0

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;
    }
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263

3 Answers3

3

Due to Type Erasure, the technique used to implement generics in Java, there is no difference between these code snippets at runtime.

However, at compile time the second code snippet provides better code safety.

For example, it ensures that all objects inside the array datastore have the same type. The first snippet lets you add objects of different type to datastore, because all objects derive from Object. The second snippet, however, requires that all calls to add were supplying objects compatible with type T, the generic type parameter of the class.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
2

Genercis is compile time concept to ensure type safety. At runtime

private T[] datastore;

will be interpreted as

private Object[] datastore;

So you can say

Generic generic = new Generic(2);
generic.add("test"); //allowed

but you cannot say

Generic<Integer> generic = new Generic<Integer>(2);
generic.add("test");  // compiler error
Aniket Thakur
  • 58,991
  • 35
  • 252
  • 267
  • But only if instantiated with T as Object, obviously. – Tim Biegeleisen Apr 12 '15 at 15:04
  • @TimBiegeleisen it will be same at runtime irrespective of Generic type you provide, not just Object. Generics help in type safety at compile time. – Aniket Thakur Apr 12 '15 at 15:13
  • Yea, I know that. So you mean to say that the only difference b/w those two is, in case of Generic type, there is a safety switch that only allows the particular type of data to be entered? – Shailendra Patel Apr 12 '15 at 15:29
  • 1
    yes precisely. You know that the input you want from your user is String and store in List. If you do not provide any generic argument or Object then user can add any value (not just String) and your processing logic will go wrong there after. To prevent it you need to enable type safety by using generics. – Aniket Thakur Apr 12 '15 at 15:34
1

When type erasure begins, compiler replaces all parameters to it top bounds. For example:

class Example<T> {}

will become

class Example<Object> {}

If you define parameter with bounds, it will be something like that:

class Example<T extends Number> {} 

will become

class Example<Number> {}

Essentially, your two examples are generic: one with a pre-defined type "Object", and the second one with T extends Object type which is more concrete and secured than the first one. With the latter, you avoid unnecessary casts

Ernusc
  • 448
  • 1
  • 4
  • 14