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

Answers were Sorted based on User's Feedback



What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with ..

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

What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with ..

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

More Core Java Interview Questions

What is the difference between abstract classes and interfaces?

0 Answers  


What is a deadlock ?

5 Answers  


Can size_t be negative?

0 Answers  


what is difference between perfom() & excute() ?

2 Answers   IBM,


Explain java coding standards for variables ?

0 Answers  






What is a classloader in java?

0 Answers  


I Have a class abstract with one abstract method, so that method should override in the subclass, but i dont want to override, if i am not override what will happen? If compilation will occur then i dont want to give compilation error, then what we need to do??? See the sample program. public abstract class AbstractExample { public abstract void sampleMethod(); } public class AbstractExampleImple extends AbstractExample { }

2 Answers   Mphasis,


Is java map thread safe?

0 Answers  


What is class array in java?

0 Answers  


what is the difference between sleep() and Wait()?

10 Answers   Accenture, Amdocs,


What is Applet Flickering ?

1 Answers   Infosys, Persistent,


Draw a UML class diagram for the code fragment given below: public class StringApplet extends Applet { private Label sampleString; private Button showTheString; private ButtonHandler bHandler; private FlowLayout layout; public StringApplet() { sampleString = new Label(" "); showTheString = new Button (" Show the String"); bHandler = new ButtonHandler(); layout = new FlowLayout(); showTheString.addActionListener(bHandler); setLayout(layout); add(sampleString); add(showTheString); } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { samplestring.setText("Good Morning"); } } } Note: The methods need not be indicated on the diagram.

0 Answers  


Categories