inderjeet singh narang


{ City } mumbai
< Country > india
* Profession * lead development
User No # 37512
Total Questions Posted # 0
Total Answers Posted # 1

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 32
Users Marked my Answers as Wrong # 1
Questions / { inderjeet singh narang }
Questions Answers Category Views Company eMail




Answers / { inderjeet singh narang }

Question { Infosys, 19179 }

What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with simple example?
Thanks, Bose.


Answer

The feature of Generics in Java allows Applications to
create classes and objects that can operate on any defined
types. Programmers can now make use of the Generics feature
for a much better code. There is no need for un-necessary
casting when dealing with Objects in a Collection.
Example without using generics
// Removes 4-letter words from c. Elements must be strings
static void expurgate(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (((String) i.next()).length() == 4)
i.remove();
}

Here is the same example modified to use generics:

// Removes the 4-letter words from c
static void expurgate(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (i.next().length() == 4)
i.remove();
}


Is This Answer Correct ?    32 Yes 1 No