25.02.2017 Views

java_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java<br />

Example<br />

Following example illustrates how we can define a generic class:<br />

public class Box {<br />

private T t;<br />

public void add(T t) {<br />

}<br />

this.t = t;<br />

public T get() {<br />

}<br />

return t;<br />

public static void main(String[] args) {<br />

Box integerBox = new Box();<br />

Box stringBox = new Box();<br />

integerBox.add(new Integer(10));<br />

stringBox.add(new String("Hello World"));<br />

}<br />

}<br />

System.out.printf("Integer Value :%d\n\n", integerBox.get());<br />

System.out.printf("String Value :%s\n", stringBox.get());<br />

This will produce the following result:<br />

Integer Value :10<br />

String Value :Hello World<br />

460

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!