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 / vamsi

Generics can accept any datatype.
For example if the method write()takes integer type,but we
send them strings,is it possible for the mehod it takes
general variables,but it is possible using generics in java.


just use 'T' Synbol infront of ur method
eg- <T> write();

Not only for method it will be used in front ofclasses


<T>class gen{

int t1=1;
int t2=2;

String s1="generics"
String s2="example";

<T> write(<T> arg1,<T> arg2){
this.arg1=arg1;
this.arg2=arg2;
}
void display()
{
System.out.println(arg1+" "+arg2);
}
}

class example{
public static void main(String args[])
{
Gen g= new Gen();
g.display(t1,t2);
g.display(s1,s2);
}
}

Output is 1 2
Generics example

note# just i told u what i have known,if any thing is wrong
in this just tell me what is the corect way.

Thank u,
vamsi.

Is This Answer Correct ?    13 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can we create a synchronized collection from given collection?

1038


What is the synchronized method modifier?

1133


What is logical variable?

921


What is time complexity java?

947


What is update method called?

1090


Explain public static void main(string args[]) in java.

931


How can you make sure that your singleton class will always return single instance in multi-threaded environment?

963


What is the difference between method and means?

1016


Is void a keyword in java?

964


How many ways can you break a singleton class in java?

915


How to change the priority of thread or how to set priority of thread?

980


Explain yield() method in thread class ?

1002


What are the differences between string, stringbuffer and stringbuilder?

966


what is object slice?

2000


What is rmi and steps involved in developing an rmi object?

992