Click here to watch in Youtube :
https://www.youtube.com/watch?v=x5UTvcx9lvg&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial: Generics in java | Java Generics [Generic types] |
Java Tutorial: Generics in java | Java Generics [Generic types] |
Java Tutorial: Generics in java | Java Generics [Generic types] |
Java Tutorial: Generics in java | Java Generics [Generic types] |
/**
* Generic version of the Box class.
*
* @param <T>
* the type of the value being boxed
*/
public class Box<T>
{
// T stands for "Type"
private T t;
public void set(T t)
{
this.t = t;
}
public T get()
{
return t;
}
}
GenericDemo.javapublic class GenericDemo
{
public static void main(String[] args)
{
Box<Integer> integerBox = new Box<Integer>();
integerBox.set(new Integer(100));
//integerBox.set("Ram"); //Compile time error
Integer integerValue = integerBox.get();
System.out.println("integerValue = "+integerValue);
Box<String> stringBox = new Box<String>();
stringBox.set("Peter");
String strValue = stringBox.get();
System.out.println("strValue = "+strValue);
}
}
OutputintegerValue = 100
strValue = Peter
Click the below link to download the code:
No comments:
Post a Comment