Source of BoxGenericDemo.java


  1: //BoxGenericDemo.java
  2: //Demonstrates the BoxGeneric<T> class by creating two
  3: //different class objects, one with an Integer private
  4: //data member, and one with a String private data member.
  5: //Note: No cast is required when retrieving the stored value.
  6: 
  7: public class BoxGenericDemo
  8: {
  9:     public static void main(String args[])
 10:     {
 11:         System.out.println();
 12: 
 13:         BoxGeneric<Integer> intObject = new BoxGeneric<>(123);
 14:         intObject.showType();
 15:         int intValue = intObject.getData(); //No cast required
 16:         System.out.println("value: " + intValue);
 17: 
 18:         System.out.println();
 19: 
 20:         BoxGeneric<String> stringObject = new BoxGeneric<>("Have a nice day!");
 21:         stringObject.showType();
 22:         String stringValue = stringObject.getData(); //No cast required
 23:         System.out.println("value: " + stringValue);
 24: 
 25:         //intObject = stringObject;  //Does not compile!
 26:     }
 27: }
 28: 
 29: /*Output:
 30: 
 31: The type of t is java.lang.Integer.
 32: value: 123
 33: 
 34: The type of t is java.lang.String.
 35: value: Have a nice day!
 36: */