Monday, 15 May 2017

Java Tutorial: Generics in java | Java Generics [Generic types]


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] 
Box.java
/**
 * 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.java
public 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);
        
    }

}
Output
integerValue = 100
strValue = Peter

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsDemo_GenericTypes_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsDemo_GenericTypes_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/47d09ebb24294f7b507419345f38ec4cde27d544/BasicJava/GenericsDemo_GenericTypes_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment