2011年10月20日 星期四

Java SE notes

notes for: http://download.oracle.com/javase/tutorial/java/TOC.html

1. emun type

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

use:
System.Out.Println("%d",Day.SUNDAY); //output 0;

2.Annotations
@Deprecated: indicate that element is deprecated. Compiler will give warning if this is used.
@SuppressWarnings: force compiler to stop compiling is this elements is used.

3.Interfaces
-are contracts; force class to implement the method.
-[used as type] ie. public CharSequence subSequence(int start, int end);
-are not part of the class hierarchy.
-interface can only be extended by other interfaces.
-can only be implemented by other classes.
-only contains constants and method
-cannot be instantiated

4.Inheritance

-Polymorphism

5.generics
-define type parameter names that prevent runtime error(passing wrong type to object)
-type parameter names are single, uppercase letters (commonly use only)
-Common type
* E - Element (used extensively by the Java Collections Framework)
* K - Key
* N - Number
* T - Type
* V - Value
* S,U,V etc. - 2nd, 3rd, 4th types
-<<<<<<>>>>>


public class Box {
private T t; // T stands for "Type"
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}

case 1

Box box=new Box();
box.add("1"); //able to compile. Have runtime error as it pass String instead of Integer.

case 2

Box box=new Box();
box.add("1"); //compiler will find this error.

6.Wildcards


7.Packages
-you can write your own classes that contain constants and static methods that you use frequently, and then use the static import statement.
ie: import static mypackage.MyConstants.*;


interface VS abstract class:
ref:http://www.cnblogs.com/oomusou/archive/2007/05/07/738311.html
-interface: methods & constants only.
-abstract: can have body. the abstract must be overrided by subclass.