jozencl jozencl - 5 months ago 13
Java Question

Generics Vs. Arraylist?

I am a little confused regarding ArrayLists and generics, for instance, in the program below when I declared,

Gen<Integer>iOb=new Gen<Integer>(88);
it declared a generic type of Integer correct? However, if I declare an arraylist in the same fashion? In the case of an arraylist, it is the type that is in angle brackets, however, researching generics, it says that the type in the angle brackets is the generic type? How do I know if its an arraylist of a class type or a generic?

//A simple generic class
//Here, T is a type parameter that
///will be replaced by a real type
//when an object of type gen is created
class Gen<T> {
T ob;//declare an object of type T

//Pass the constructor a refernce to
//an object of type T
Gen(T o) {
ob = o;
}

//return ob
T getob() {
return ob;
}

//show type of t
void showType() {
System.out.println("Type of T is " + ob.getClass().getName());
}
}

class GenDemo {
public static void main(String[] args) {
//create a gen reference for integers
Gen<Integer> iOb;
//Create a Gen<Integer>Object and assign its
//reference to iob, notice the use of autoboxing
//to encapsulate the value 88 within an integer object
iOb = new Gen<Integer>(88);

//show the type off data used by iob
iOb.showType();
//get the value in Iob notice that no cast is needed
int v = iOb.getob();
System.out.println("Value: " + v);
System.out.println();
//create a gen object for strings
Gen<String> strOb = new Gen<String>("Generic Test");
//show the type of data
strOb.showType();
//get the value of strOb, again notice that no cast is needed
String str = strOb.getob();
System.out.println("Value :" + str);
}
}

Answer

When I declared, Gen<Integer> iOb = new Gen(88); it declared a generic type of Integer correct?

This is incorrect. You are declaring/creating an Gen object, which holds a T extends Object. Inside Gen code, you can only use functions that are allowed to be used on an Object, such as getClass().

The Java Compiler remembers that iOb will reference a Gen object that must hold an Integer object, and will generate automatic type casting on the return value of getob() ...

int v=iOb.getob();

Is interpreted by the compiler as if the following was written:

int v = (Integer) iObj.getob();

Where getob() is considered to just return an Object. Note that this is a compile-time transformation as a convenience for the programmer.

However, if I declare an arraylist in the same fashion? In the case of an arraylist, it is the type that is in angle brackets, however, researching generics, it says that the type in the angle brackets is the generic type? How do I know if its an arraylist of a class type or a generic???

class SomeClass<T> { ... } declares a generic type. The part between the angle brackets is the "type argument(s)" for the generic class.

When the generic class used as a type for a variable, it is considered a specialization of the the generic class, and type argument(s) could be a concrete class, an interface, or another generic class.