Review Java advanced features
http://download.oracle.com/javase/6/docs/api/
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
- 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>();
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”.
Sample:public static <T extends Comparable> T min(T[] a) . . .
translate generics:
- Type Parameters Cannot Be Instantiated with Primitive Types (double, int, ...: eight primitive types)
- Runtime Type Inquiry Only Works with Raw Types
- You Cannot Throw or Catch Instances of a Generic Class
- Arrays of Parameterized Types Are Not Legal
- You Cannot Instantiate Type Variables
- Type Variables Are Not Valid in Static Contexts of Generic Classes
- Beware of Clashes After Erasure
- Inheritance Rules for Generic Types
- Wildcard Types
1.1.1 Sample 1
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 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);
}
}
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 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:
value= 88
----------------------------------
T的实际类型是: java.lang.String
value= Hello Gen!
1.2 Generic method sample
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
A Simple Example
To see how reflection works, consider this simple example:
import java.lang.reflect.*;
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);
}
}
}
java DumpMethods java.util.Stack
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)