Lokvin Wiki
Line 60: Line 60:
   
 
=== Enhance for Loop ===
 
=== Enhance for Loop ===
  +
* The new enhanced for loop provides a simple, consistent syntax for iterating over collections and arrays.
  +
 
'''old style: '''
 
'''old style: '''
 
<source lang="java">
 
<source lang="java">

Revision as of 14:37, 16 November 2013

doc & reference

Singleton

static synchronized method

  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() {
      synchronized(this){
      
      } 

  }
}

HashMap

Java 5 new features

Generics

  • 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

  • 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];
    System.out.println(t);
}

new style:

String[] arr = new String[]{"alpha", "beta"};
for (String s : arr) {
    System.out.println(s);
}