Escolar Documentos
Profissional Documentos
Cultura Documentos
GENERIC IN
JAVA, WHY AND
WHERE
HISTORY
For the first time this concept used in Ada in 1983, it allows you to use
the same functions or types for different data types.
Using generic in programming help you to maintenance easier and
reduce you source code.
For those people that have worked with C++ and are familiar with C+
+, the generic type in Java is similar with the Template in C++ with
some differences.
It allows you to define a class or function and use it for every different
data types.
Generic wasn’t possible before java 5, I usually used data type of
Object to use some similarly Template functionality in earlier java
version, which it wasn’t without problem. We will see some example
here and see why the data-type of Object doesn’t work.
And typically example is using List.
I will describe more about it in this paper.
TEMPLATE IN C++
An example on how you can use template in c++ can be:
Page 2
generic using in java and why
public:
void Add(T const &d);
void Remove();
void Print();
};
As you can see you can add integer, string, or other data type in this
Queue-system .
you can use exactly the same class and use class instead typename.
Page 3
generic using in java and why
And you shouldn’t thin about what happened in compiler, but if you
curious, the compiler just generate for the data type that you have
used it.
It should be very easier to debug and it reduce your code line.
Page 4
generic using in java and why
Page 5
generic using in java and why
And finally in line3, you cast of value list.get(0) to integer, which is not valid.
The complier shouldn’t generate any Error, but in runtime you will get an exception on
line three for classcastException (java.lang.classcastexception).
To avoid this kind of runtime-error you can use the Generic as follows:
Page 6
generic using in java and why
One the most important of advantages of Generic in Java is, you can
use the wildcards to define your type of parameters by just a question
mark “?”.
For example if you use <?> instead in example above, it means that
the datatype is unknown and function of this type of parameter can get
any type of list, regardless of parameter’s type. One thing you should
remember that, the function return Object-type. And the second you
should remember that none-null elements are not allowed used with
Generic.
You can use the extend keyword to define the upper bound of a
Generic Element as follows:
List <? Extend Number>, and means that List can be float, integer or
class Number, and it will return the Number. You should remember
that you cannot convert from a type to an other type, since it sounds
that float and integer are sub type of Number.
And exactly at the same way, if you want define the lower bound of a
Generic Element you can use the super keyword as follows:
Page 7
generic using in java and why
AN EXAMPLE
If we look at the following example we can see better how we can use
Generic in Java.
There are two files, on interface and on implementation of interface.
I have just make a very simple interface which it has just one function,
and it will print out the type of class we define or use in our
application.
package martin.test.generic.example2;
Page 8
generic using in java and why
And in the second case I made a new object of the same class and give
it Integer as parameter and when we look at the type of the class, it
will show it is Integer.
Very simple you can use and make you own define classes and
function by using Generic.
///////////////////////////////////////////////
package martin.test.generic.example2;
/**
* @param args
*/
public static void main(String[] args) {
MainGetClassType<String> mainGetClassType = new
MainGetClassType<String>();
mainGetClassType.printClassType(new String("test"));
mainGetClassType.printClassType();
Page 9
generic using in java and why
}
public void printClassType() {
System.out.println(this.getClass());
package martin.test.generic.example3;
one = o;
two = t;
three = m;
public O getOne(){
return one;
public T getTwo(){
return two;
Page 10
generic using in java and why
public M getThree;
private O one;
private T two;
private M three;
package martin.test.generic.example3;
/**
* @param args
*/
System.out.println("StringStringString:" +
genericTest3StringString.toString());
System.out.println("StringIntegerString:" +
genericTest3StringInteger.toString());
Page 11
generic using in java and why
Or in our example:
Public GenericTest3<String, String, String> test(String s1, String s2,
String3){
Return new GenericTest3<String, String, String>(s1,s2,s3);
}
or
Public GenericTest3<String, Integer, String> test(String s1, Integer i1,
String3){
Return new GenericTest3<String, String, String>(s1,i1,s3);
}
To be continue….
Page 12
generic using in java and why
Page 13