Source of IntSumWithoutWildcardDemo.java


  1: //IntSumWithoutWildcardDemo.java

  3: public class IntSumWithoutWildcardDemo
  4: {
  5:     public static void main(String args[])
  6:     {
  7:         System.out.println();

  9:         Integer intNums[] =
 10:         {
 11:             1, 2, 3, 4, 5
 12:         };
 13:         IntSumWithoutWildcard<Integer> intObject
 14:             = new IntSumWithoutWildcard<>(intNums);
 15:         int iSum = intObject.intSum();
 16:         System.out.println("intObject sum is " + iSum);

 18:         Double doubleNums[] =
 19:         {
 20:             1.1, 2.2, 3.3, 4.4, 5.5
 21:         };
 22:         IntSumWithoutWildcard<Double> doubleObject
 23:             = new IntSumWithoutWildcard<>(doubleNums);
 24:         iSum = doubleObject.intSum();
 25:         System.out.println("doubleObject sum is " + iSum);

 27:         Float floatNums[] =
 28:         {
 29:             1.1f, 2.2f, 3.3f, 4.4f, 5.5f
 30:         };
 31:         IntSumWithoutWildcard<Float> floatObject
 32:             = new IntSumWithoutWildcard<>(floatNums);
 33:         iSum = floatObject.intSum();
 34:         System.out.println("floatObject sum is " + iSum);

 36:         System.out.println();

 38:         //This attempt to compare two integer sums compiles and runs only
 39:         //because the two objects in the second call to println() are of
 40:         //the same type:
 41:         Integer otherIntNums[] =
 42:         {
 43:             11, 1, 1, 1, 1
 44:         };
 45:         IntSumWithoutWildcard<Integer> otherIntObject
 46:             = new IntSumWithoutWildcard<>(otherIntNums);
 47:         iSum = otherIntObject.intSum();
 48:         System.out.println("otherIntObject sum is " + iSum);
 49:         System.out.print("The sums of intObject and otherIntObject are ");
 50:         System.out.println
 51:         (
 52:             intObject.hasSameIntSum(otherIntObject) ? "the same." : "different."
 53:         );

 55:         //But the following three comparison attemps will not compile because
 56:         //in each call to println() the two objects are of different types:
 57:         /*
 58:             System.out.print("The sums of intObject and doubleObject are ");
 59:             System.out.println(intObject.hasSameIntSum(doubleObject)
 60:              ? "the same." : "different.");
 61:             System.out.print("The sums of intObject and floatObject are ");
 62:             System.out.println(intObject.hasSameIntSum(floatObject)
 63:              ? "the same." : "different.");
 64:             System.out.print("The sums of doubleObject and floatObject are ");
 65:             System.out.println(doubleObject.hasSameIntSum(floatObject)
 66:              ? "the same." : "different.");
 67:         */
 68:     }
 69: }