Review Java advanced features

http://www.oracle.com/technetwork/java/javase/documentation/api-jsp-136079.html 

http://download.oracle.com/javase/6/docs/api/

 
Here the topics that you should master as part of your advance java study.

    Generics
        Introduction to Generics
        Type Erasure
        Type Boundaries
        Wildcards
        Generic Methods
        Strengths and Weaknesses of Generics
        Legacy Code and Generics
    Threads
        Java Thread Model
        Creating and Running Threads
        Manipulating Thread State
        Thread Synchronization
        Volatile Fields vs. Synchronized Methods
        wait and notify
        join and sleep
        The Concurrency API
        Atomic Operations
    Reflection
        Uses for Meta-Data
        The Reflection API
        The Class<T> Class
        The java.lang.reflect Package
        Reading Type Information
        Navigating Inheritance Trees
        Dynamic Instantiation
        Dynamic Invocation
        Reflecting on Generics
    Annotations
        Aspect-Oriented Programming and Java
        The Annotations Model
        Annotation Types and Annotations
        Built-In Annotations
        Annotations vs. Descriptors (XML)
    Sockets
        The OSI Reference Model
        Network Protocols
        The Socket Class
        The ServerSocket Class
        Connecting Through URL Objects
        HTTP and Other TCP Servers
        Datagram Clients and Servers
        Non-Blocking Sockets
    Database and SQL Fundamentals
        Relational Databases and SQL
        Database, Schema, Tables, Columns and Rows
        SQL Versions and Vendor Implementations
        DDL -- Creating and Managing Database Objects
        DML -- Retrieving and Managing Data
        Sequences
        Stored Procedures
        Using SQL Terminals
    JDBC Fundamentals
        What is the JDBC API?
        JDBC Drivers
        Making a Connection
        Creating and Executing a Statement
        Retrieving Values from a ResultSet
        SQL and Java Datatypes
        Creating and Updating Tables
        Handling SQL Exceptions and Proper Cleanup
        Handling SQLWarning
    Advanced JDBC
        SQL Escape Syntax
        Using Prepared Statements
        Using Callable Statements
        Scrollable Result Sets
        Updatable Result Sets
        Transactions
        Commits, Rollbacks, and Savepoints
        Batch Processing
    Introduction to Row Sets
        Row Sets in GUI and J2EE programming
        Advantages of RowSets
        RowSet Specializations
        Using CachedRowSets
    Design Patterns
        What are Design Patterns?
        Singleton, Factory Method, Abstract Factory
        Adapter, Composite, Decorator
        Chain of Responsibility, Observer / Publish-Subscribe, Strategy, Template
        Data Access Object (DAO)

1. Generics: type parameter <>, wildcard type <?>, Bounds for Type Variables, type erasure

Generics allow "a type or method to operate on objects of various types while providing compile-time type safety."
  • A type variable is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
  • A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.
  • An interface is generic if it declares one or more type variables. These type variables are known as the type parameters of the interface. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.
  • A method is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.
  • A constructor can be declared as generic, independently of whether the class of the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface.

Type parameter:

Before Java SE 5.0, generic programming in Java was always achieved with inheritance.
The ArrayList class simply maintained an array of Object references:
    public class ArrayList // before Java SE 5.0
    {
        public Object get(int i) { . . . }
        public void add(Object o) { . . . }
        . . .
        private Object[] elementData;
    }
This approach has two problems. A cast is necessary whenever you retrieve a value:
    ArrayList files = new ArrayList();
    . . .
    String filename = (String) names.get(0);
Moreover, there is no error checking. You can add values of any class:
    files.add(new File(". . ."));
This call compiles and runs without error. Elsewhere, casting the result of get to a String
will cause an error.
Generics offer a better solution: type parameters. The ArrayList class now has a type
parameter that indicates the element type:
    ArrayList<String> files = new ArrayList<String>();

wildcard type:
The ArrayList class has a method addAll to add all elements of another collection. A programmer may want to add all elements from an ArrayList<Manager> to an ArrayList<Employee>. But, of course, doing it the other way around should not be legal. How do you allow one call and disallow the other? The Java language designers invented an ingenious new concept, the wildcard type, to solve this problem. Wildcard types are rather abstract, but they allow a library builder to make methods as flexible as possible.

NOTE: It is common practice to use uppercase letters for type variables, and to keep them short. The Java library uses the variable E for the element type of a collection, K and V for key and value types of a table, and T (and the neighboring letters U and S, if necessary) for “any type at all”.

C++ NOTE: Superficially, generic classes in Java are similar to template classes in C++. The only obvious difference is that Java has no special template keyword. However, as you will see throughout this chapter, there are substantial differences between these two mechanisms.
bounds for type variables:

Sample:public static <T extends Comparable> T min(T[] a) . . .

A type variable or wildcard can have multiple bounds. For example: T extends Comparable & Serializable

translate generics:

NOTE: The virtual machine does not have objects of generic types—all objects belong to ordinary classes. So compiler inserts casts to translate generic calls to virtual machine instructions.
type erasure:
Your programs may contain different kinds of Pair, such as Pair<String> or Pair<Gregorian-Calendar>, but erasure turns them all into raw Pair types. 

Restrictions and Limitations:
  1. Type Parameters Cannot Be Instantiated with Primitive Types (double, int, ...: eight primitive types)
  2. Runtime Type Inquiry Only Works with Raw Types
  3. You Cannot Throw or Catch Instances of a Generic Class
  4. Arrays of Parameterized Types Are Not Legal
  5. You Cannot Instantiate Type Variables
  6. Type Variables Are Not Valid in Static Contexts of Generic Classes
  7. Beware of Clashes After Erasure
  8. Inheritance Rules for Generic Types
  9. Wildcard Types
 
1.1 Generic class sample 

1.1.1 Sample 1

class Gen<T> {
    private T ob; //定义泛型成员变量
    public Gen(T ob) {
        this.ob=ob;
    }
    public T getOb() {
        return ob;
    }
    public void setOb(T ob) {
        this.ob = ob;
    }
    public void showType() {
        System.out.println("T的实际类型是: " + ob.getClass().getName());
    }
}
public class GenDemo {
    public static void main(String[] args) {
        //定义泛型类Gen的一个Integer版本
        Gen<Integer> intOb=new Gen<Integer>(88);
        intOb.showType();
        int i= intOb.getOb();
        System.out.println("value= " + i);
        System.out.println("----------------------------------");
        //定义泛型类Gen的一个String版本
        Gen<String> strOb=new Gen<String>("Hello Gen!");
        strOb.showType();
        String s=strOb.getOb();
        System.out.println("value= " + s);
    }
}


1.1 Sample 2
class Gen2 {
    private Object ob; //定义泛型成员变量
    public Gen2(Object ob) {
        this.ob=ob;
    }
    public Object getOb() {
        return ob;
    }
    public void setOb(Object ob) {
        this.ob = ob;
    }
    public void showType() {
        System.out.println("T的实际类型是: " + ob.getClass().getName());
    }
}
public class GenDemo2 {
    public static void main(String[] args) {
        //定义泛型类Gen的一个Integer版本
        Gen2 intOb=new Gen2(new Integer(88));
        intOb.showType();
        int i= (Integer)intOb.getOb();
        System.out.println("value= " + i);
        System.out.println("----------------------------------");
        //定义泛型类Gen的一个String版本
        Gen2 strOb=new Gen2("Hello Gen!");
        strOb.showType();
        String s=(String)strOb.getOb();
        System.out.println("value= " + s);
    }
}

Both samples' result is:

T的实际类型是: java.lang.Integer
value= 88
----------------------------------
T的实际类型是: java.lang.String
value= Hello Gen!

1.2 Generic method sample

public class genericMethod {
    public static <T> T getMiddle(T[] a){
        return a[a.length/2];
    }
    public static void main(String[] args) {
        String[] names = { "John", "Mark", "Public" };
        String middle = genericMethod.<String>getMiddle(names);
        System.out.println(middle);
    }
}
 

2. Reflection  

http://download.oracle.com/javase/tutorial/reflect/ 
Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.
Java reflection relative API is in package java.lang.reflect.

A Simple Example

To see how reflection works, consider this simple example:

    import java.lang.reflect.*;
    public class DumpMethods {
      public static void main(String args[])
      {
         try {
            Class c = Class.forName(args[0]);
            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
            System.out.println(m[i].toString());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }
For an invocation of:

  java DumpMethods java.util.Stack
the output is:

  public java.lang.Object java.util.Stack.push(
    java.lang.Object)
   public synchronized
     java.lang.Object java.util.Stack.pop()
   public synchronized
      java.lang.Object java.util.Stack.peek()
   public boolean java.util.Stack.empty()
   public synchronized
     int java.util.Stack.search(java.lang.Object)
   
原文地址:https://www.cnblogs.com/markjiao/p/2239164.html