Generics in Java #Java Short Notes

  • Generics can prevent many run time errors.
  • A generic class:
    public class Box {    private T t; // T stands for "Type"              public void add(T t) {        this.t = t;    }    public T get() {        return t;    }}Object creation:
    Box integerBox = new Box();
  • Generic method syntax: public void inspect(U u){}
  • Type Erasure:Check this code:
    public class MyClass {    public static void myMethod(Object item) {        if (item instanceof E) {  //Compiler error            ...        }        E item2 = new E();   //Compiler error        E[] iArray = new E[10]; //Compiler error        E obj = (E)new Object(); //Unchecked cast warning    }}

From: http://sitestree.com/?p=4902
Categories:Java Short Notes
Tags:
Post Data:2007-10-28 07:08:01

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada