Reputation: 1218
I was wondering if there is any significant advantages/disadvantages between implementing a class using Java generic programming and keeping the types as general Object class? Maybe the codes below clarifies what I am looking for?
This first code shows the use of general Object class type to implement a list:
public class myList {
private static int DEFAULT_CAPACITY = 16;
private int size, capacity;
private Object[] dataArray;
myList(){
this(DEFAULT_CAPACITY);
}
myList(int defaultSize){
capacity = defaultSize;
size = 0;
dataArray =new Object[defaultSize];
}
public int getSize(){return size;}
public void insert(int loc, Object o){
if (size+1 <= capacity){
for (int i=size; i> loc-1; i--)
dataArray[i] = dataArray[i-1];
dataArray[loc-1] = o;
size++;
}
}
public Object getItem(int loc){
return dataArray[loc-1];
}
}
and the following code shows the same functionality using Java generic programming:
public class MyList<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object dataArray[];
public MyList() {
dataArray = new Object[DEFAULT_CAPACITY];
}
public void insert(int loc, E e) {
if (size+1 <= capacity){
for (int i=size; i> loc-1; i--)
dataArray[i] = dataArray[i-1];
dataArray[loc-1] = e;
size++;
}
}
public E get(int loc) {
return (E) elements[loc-1];
}
}
Upvotes: 0
Views: 462
Reputation: 1447
The important keyword is "Design by Contract". Generics allow you to validate the contract "the elements in this list have this type" even before compilation - all you need is your abstract syntax tree. Type validation is imo the major advantage of strongly typed languages.
It also makes code completion and refractorings easier.
Upvotes: 0
Reputation: 33534
Let me break this up for u...............
- Generics
can be implemented to class, interface, methods, fields but its most important usage is for making the Collection
type safe.
- This is how Collections
are made type-safed in Java, so a wrong type doesn't enter into the Collection, As collection are checked only during the Compilation
time and Not during the Runtime
.. ie a Cat object should not enter into a Collection of type Dog.
- There is a process known as Erasure
, where the compiler removes the Type Parameters and Type Arguments from the Generics class during Compilation time, making it of Raw type during the Run-Time, In order to maintain a backward compatibility with the code that were written WithOut Generics.
Eg:
Box<String>
becomes Box
Upvotes: 1
Reputation: 41200
First your generic MyList<E>
's dataArray should be generic.
class MyList<E> {
private E dataArray[];
public MyList(Class<E> c,int size) {
dataArray = (E[]) Array.newInstance(c,size);
}
}
Advantage - your List will be type safe in nature means in general object type you can add any type which could be caused problematic when you do operation like sorting etc...
Upvotes: 1
Reputation: 46219
The advantage with the Generic
approach is this:
Say that you create a list of Integer
.
With the Generic
approach, if you try to put something else into the list, you will get an error at compile time.
With the Object
approach, you will not see this error until runtime, which is a recipe for really hard-to-find bugs.
Upvotes: 3