Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Answer Posted / inder_gwl

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<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); )
if (i.next().length() == 4)
i.remove();
}

Is This Answer Correct ?    32 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is 16 bits called?

888


Is there any difference between nested classes and inner classes?

1027


Hi Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..

2236


What is the purpose of assert keyword used in jdk1.4.x?

1009


What do you understand by abstract classes?

1017


List the features of java programming language.

972


What is preparedstatement in java?

978


What is array size in java?

916


What is meant by javabeans?

1027


What do you mean by data type?

970


What is the difference between overriding and overloading in OOPS.

1073


Is 0 true or is 1 true?

902


Define max and min heap, also the search time of heap.

987


which pattern is default in scanner package?

2186


What is a short in java?

962