Professional Documents
Culture Documents
The Generics permits us to develop classes and objects that can work on any deIined types.
In general words, Generics allows us to create a single method Ior sorting that can be used to
sort Integer array, a String array or any type that supports ordering. Similarly, a generic class
allows us to use this class Ior any deIined types.
Generics permits us to catch the incompatible types during compilation. The main reason oI
using Generics code is to assures type saIety. It means that types which are required Ior
Application and the types which is sent by the client, must match. II there is diIIerence or match
is incompatible then it will leads to error. To ensure this match or compatibility we use Generics.
In Generics, classes and methods are deIined with parametric type. You can replace a suitable
java type during compilation with the help oI these parametric type. This saves us Irom un-
necessary type casting and also prevent us Irom using wrong or incompatible data types.
efore Generics
Map directory = new HashMap();
directory.put(new Long(9568575757L), "Sameer");
directory.put(new Long(9587654757L), "Joy");
Set dirValues = directory.entrySet();
Iterator dirIterator = dirValues.iterator();
while (dirIterator.hasNext())
,
Map.Entry anEntry = (Map.Entry)dirIterator.next();
Long number = (Long)anEntry.getKey();
String name = (String)anEntry.getValue();
System.out.println(number + ":" + name);
,
fter Generics
Map<Long, String directory = new HashMap<Long, String();
directory.put(new Long(9568575757L), "Sameer");
directory.put(new Long(9587654757L), "Joy");
Set<Map.Entry<Long, String dirValues = directory.entrySet();
Iterator<Map.Entry<Long, String dirIterator = dirValues.iterator();
while (dirIterator.hasNext())
,
Map.Entry<Long, String anEntry = dirIterator.next();
Long number = anEntry.getKey();
String name = anEntry.getValue();
System.out.println(number + ":" + name);
,
Java Generics
eIore the advent oI Java 5 we had only Arrays, Properties, and HashMap. ut with Java 5 and
generics Ieatures, building a ValuePair has become simpler. In Iact generics is the concept that
generalizes the use oI variables. The situation with generics is quite simple but building classes
with them is a tuII task. The syntax is complex and the abstraction level is high.
Java generics takes several steps to use them:
Iirst deIine an interIace:
public interIace
CoupleP, Q~
public P getIndex();
public Q
getValue();
}
The code is simple but slightly diIIerent iI the developer is unknown with the Java 5 syntax. The
Iirst thing that is new to the developer is the use oI P, Q~. These are nothing but the generic
types that has to be deIined later. These types are not known in advance and is available at
compile time (at the time oI declaration oI these variables). To clearly understand lets see the
implementation:
public class ValuePairA, ~
implements CoupleA, ~
private A a;
private b;
public ValuePair(A a, b)
this.a a;
this.b b;
}
public A getIndex()
return a;
}
public getValue()
return b;
}
}
Now there should not have much more conIusion about generics. Since the class includes the
generiIied interIace thereIore it also includes the generic types. We can use these generic types oI
any two diIIerent types (such as int and Iloat, long and int or any combination oI two diIIerent
types.) Things are going to be more interested while subclassing a base class.
public class NameValuePairT~ extends
ValuePairString, T~
public NameValuePair(String a, T b)
super(a, b);
}
public String getName()
return getIndex();
}
}
From the above code it is clearly understood that only one parameter is required because other
parameter is already deIined as the String.
You have seen the advantages oI using generic classes. We can use them as a Iamily oI variables
according their signature.
:toboxing and Unboxing
The :toboxing and Unboxing was released with the Java 5.
:toboxing
During assignment, the automatic transIormation oI primitive type(int, Iloat, double etc.) into
their object equivalents or wrapper type(Integer, Float, Double,etc) is known as :toboxing.
During assignment or calling oI constructor, the automatic transIormation oI wrapper types into
their primitive equivalent is known as Unboxing.
onversion of int into Integer
int inative = 0;
inative = new Integer(5); // auto-unboxing
Integer intJbject = 5; // autoboxing
$toring int a into vector vt
int a = 10;
Vector <integer vt = new Vector <integer ();
vt.add(a);
int n = vt.elementAt(0); </integer</integer
:toboxing also works with comparison
int a = 10;
Integer b = 10;
System.out.println(a==b);
Output oI the above code will be :
CuLpuL
Lrue