doc & reference Edit


static synchronized methodEdit

  1. difference between static synchronized method and non-static synchronized method
  • The lock objects are difference between static synchronized method and non-static synchronized method. Static synchronized method lock object is class object (lock object: FooClass.class), non-static synchronized lock object is instance object (lock object: this).
   public class Foo {
           public static synchronized void methodA() {}
           public synchronized void methodB() {}

It's roughly equals

public class Foo {
  public static void methodA() {
    synchronized(Foo.class) {
  public void methodB() {

HashMap Edit

Java 5 new features Edit

Generics Edit

  • Add compile time type safe
  • Elimination drudgery class casting

old style:

List list = new ArrayList();
String s = (String)list.get(0);

new style:

List<String> list = new ArrayList<String>();
String s = list.get(0);

Enhance for Loop Edit

  • The new enhanced for loop provides a simple, consistent syntax for iterating over collections and arrays.

old style:

String[] arr = new String[]{"alpha", "beta"};
for (int i=0; i<=arr.length; i++) {
    String t = arr[i];

new style:

String[] arr = new String[]{"alpha", "beta"};
for (String s : arr) {
  • some limitation of enhance loop, some time we need index and iterator

example need index:

for (int i=0; i<numbers.length; i++) {
  if (i !=0 && i != numbers.length - 1) {

example need iterator:

for (Iterator<String> it = n.iterator(); it.hasNext(); ) {
    if ( < 0) it.remove();

Auto Boxing/ Auto un-boxing Edit

  • auto conversion between primitives and wrappers

Type safe Enums Edit

  • Enums are type safe.
  • Enums are Serializable and Comparable by default
  • Programmers doesn’t require to do extra work of implementing toString(), equals() and hashCode(). Enums provide implementation of these by default.
  • Programmers can use Enums in switch-case statements.
  • Enums are permitted to implement to interfaces.

old style:

public class Dialog {
    public static final int SHORT = 0;
    public static final int LONG = 1;
    public static void show(int duration) {
        if(duration == SHORT) ...
        else if (duration == LONG) ...

new style:

public class Dialog {
    public enum Duration {LONG, SHORT};
    public static void show(Duration e) {
        if (e == Duration.SHORT) ...
        else if (e == Duration.Long) ...

Varargs Edit

  • Variable size argument lists for method
void orderCar(int modelNo, String... options) {}
orderCar(Benz.E_CLASS, "navigation", "hi pass");
public void foo(int i, String... strings) {
    	  String[] someStrings = strings;
    	  // rest of method body

Annotation (metadata) Edit

  • attach extra information about code
    • compiler check
    • code analysis
public boolean equals(Object obj)
public doSomething() ...
public static void selfDestruct() {

static import Edit

  • avoiding qualifying static members

formated output Edit

  • printf(), String.format();
System.out.printf("Hello %s %s, "world", "hi");
String s = String.format("The meaning of %s", "right");

a Iterator with predicate Edit

  • FilterIterator in commons cllection

java finalize method Edit

java enum Edit

java annotation Edit

java reflection & introspection Edit

Decorator design pattern Edit

java anonymous inner class Edit

apache commons beanutils Edit

apache commons langEdit

java performance Edit

java default char encoding Edit

arrow operation, lambda Edit