Java generic: Use a wildcard. : Generic « Language Basics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Language Basics » GenericScreenshots 
Java generic: Use a wildcard.
Java generic: Use a wildcard.

/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/


class Stats<T extends Number> {   
  T[] nums; // array of Number or subclass  
     
  // Pass the constructor a reference to    
  // an array of type Number or subclass.  
  Stats(T[] o) {   
    nums = o;   
  }   
   
  // Return type double in all cases.  
  double average() {   
    double sum = 0.0;  
  
    for(int i=0; i < nums.length; i++)   
      sum += nums[i].doubleValue();  
  
    return sum / nums.length;  
  
 
  // Determine if two averages are the same. 
  // Notice the use of the wildcard. 
  boolean sameAvg(Stats<?> ob) { 
    if(average() == ob.average())  
      return true
 
    return false
  
}   
   
// Demonstrate wildcard. 
public class WildcardDemo {   
  public static void main(String args[]) {   
    Integer inums[] 1234};  
    Stats<Integer> iob = new Stats<Integer>(inums);    
    double v = iob.average();  
    System.out.println("iob average is " + v);  
  
    Double dnums[] 1.12.23.34.45.5 };  
    Stats<Double> dob = new Stats<Double>(dnums);    
    double w = dob.average();  
    System.out.println("dob average is " + w);  
  
    Float fnums[] 1.0F2.0F3.0F4.0F5.0F };  
    Stats<Float> fob = new Stats<Float>(fnums);    
    double x = fob.average();  
    System.out.println("fob average is " + x);  
  
    // See which arrays have same average. 
    System.out.print("Averages of iob and dob ")
    if(iob.sameAvg(dob)) 
      System.out.println("are the same.");  
    else 
      System.out.println("differ.");  
 
    System.out.print("Averages of iob and fob ")
    if(iob.sameAvg(fob)) 
      System.out.println("are the same.");  
    else 
      System.out.println("differ.");  
  }   
}


           
       
Related examples in the same category
1. A simple generic class. A simple generic class.
2. Demonstrate the non generic classDemonstrate the non generic class
3. A simple generic class with two type parameters: T and V.A simple generic class with two type parameters: T and V.
4. Java generic: Hierarchy argumentJava generic: Hierarchy argument
5. Java generic: Bounded Wildcard argumentsJava generic: Bounded Wildcard arguments
6. Demonstrate a simple generic method.Demonstrate a simple generic method.
7. Use a generic constructor. Use a generic constructor.
8. A generic interface example. A generic interface example.
9. Generic ArrayListGeneric ArrayList
10. Generic Data Structure
11. Boxing Generic Example
12. Unchecked Example
13. Interface Generic (Has compile error)
14. Generic StackGeneric Stack
15. Enum and GenericEnum and Generic
16. Generic HashMapGeneric HashMap
17. Foreach and generic data structureForeach and generic data structure
18. Stats attempts (unsuccessfully) to create a generic class
19. Java generic: Ambiguity caused by erasure on overloaded methods.
20. Java 1.5 (5.0) new feature: collection and thread
21. Demonstrate a raw generic type. Demonstrate a raw generic type.
22. A simple generic class heirarchy.A simple generic class heirarchy.
23. A nongeneric class can be the superclass of a generic subclass.A nongeneric class can be the superclass of a generic subclass.
24. Use the instanceof operator with a generic class hierarchy. Use the instanceof operator with a generic class hierarchy.
25. Overriding a generic method in a generic class. Overriding a generic method in a generic class.
26. Pre generics example that uses a collection.Pre generics example that uses a collection.
27. Data structure and collections: Modern, generics version.Data structure and collections: Modern, generics version.
28. Java hierarchy generic classJava hierarchy generic class
29. Java generic: A situation that creates a bridge method.Java generic: A situation that creates a bridge method.
30. Java generic: Generics and arrays.
31. Collections and Data structure: the generic wayCollections and Data structure: the generic way
32. Custom Generic Object TesterCustom Generic Object Tester
w___ww__.j___ava___2_s__.c_om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.