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;
    }
}
1
  • Is this a homework assignment? Google Java generics. The Sun/Oracle trails give a great basic coverage of this topic. Commented Apr 12, 2015 at 15:03

3 Answers 3

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.

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

Comments

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

4 Comments

But only if instantiated with T as Object, obviously.
@TimBiegeleisen it will be same at runtime irrespective of Generic type you provide, not just Object. Generics help in type safety at compile time.
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?
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.
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

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.