Lokvin Wiki
Register
 
(23 intermediate revisions by the same user not shown)
Line 80: Line 80:
   
 
* some limitation of enhance loop, some time we need index and iterator
 
* some limitation of enhance loop, some time we need index and iterator
  +
  +
'''example need index:'''
 
<source lang="java">
 
<source lang="java">
 
for (int i=0; i<numbers.length; i++) {
 
for (int i=0; i<numbers.length; i++) {
Line 88: Line 90:
 
}
 
}
 
</source>
 
</source>
  +
  +
'''example need iterator: '''
  +
  +
<source lang="java">
  +
for (Iterator<String> it = n.iterator(); it.hasNext(); ) {
  +
if (it.next() < 0) it.remove();
  +
  +
}
  +
</source>
  +
  +
  +
===Auto Boxing/ Auto un-boxing ===
  +
* auto conversion between primitives and wrappers
  +
  +
===Type safe Enums ===
  +
* 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:'''
  +
<source lang="java">
  +
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) ...
  +
}
  +
....
  +
Dialog.show(Dialog.LONG);
  +
}
  +
</source>
  +
  +
'''new style:'''
  +
<source lang="java">
  +
public class Dialog {
  +
public enum Duration {LONG, SHORT};
  +
public static void show(Duration e) {
  +
if (e == Duration.SHORT) ...
  +
else if (e == Duration.Long) ...
  +
}
  +
...
  +
Dialog.show(Duration.Long);
  +
  +
}
  +
</source>
  +
  +
  +
===Varargs ===
  +
* Variable size argument lists for method
  +
  +
<source lang="java">
  +
void orderCar(int modelNo, String... options) {}
  +
  +
orderCar(Benz.E_CLASS, "navigation", "hi pass");
  +
  +
</source>
  +
  +
<source lang="java">
  +
public void foo(int i, String... strings) {
  +
String[] someStrings = strings;
  +
// rest of method body
  +
}
  +
</source>
  +
  +
===Annotation (metadata) ===
  +
* attach extra information about code
  +
**compiler check
  +
**code analysis
  +
  +
<source lang="java">
  +
@Override
  +
public boolean equals(Object obj)
  +
  +
  +
@Deprecated
  +
public doSomething() ...
  +
  +
  +
@SuppressWarnings("deprecation")
  +
public static void selfDestruct() {
  +
Thread.currentThread().stop();
  +
}
  +
</source>
  +
  +
===static import ===
  +
* avoiding qualifying static members
  +
  +
===formated output ===
  +
* printf(), String.format();
  +
  +
<source lang="java">
  +
System.out.printf("Hello %s %s, "world", "hi");
  +
String s = String.format("The meaning of %s", "right");
  +
</source>
  +
  +
==a Iterator with predicate ==
  +
* FilterIterator in commons cllection <br>
  +
https://github.com/yren/java-basic/blob/master/src/main/java/com/lokvin/basic/FilterIterator.java
  +
  +
==java finalize method ==
  +
* http://howtodoinjava.com/2012/10/31/why-not-to-use-finalize-method-in-java/
  +
  +
==java enum ==
  +
* http://javahowto.blogspot.com/2008/04/java-enum-examples.html - a doc about enum
  +
* http://www.mkyong.com/java/java-enum-example/ - an example of enum constructor
  +
  +
==java annotation ==
  +
* http://en.wikipedia.org/wiki/Java_annotation
  +
* http://javapapers.com/core-java/java-annotations/
  +
  +
==java reflection & introspection ==
  +
* http://www.blogjava.net/191499719/archive/2008/07/03/212325.html - a blog about reflection
  +
* http://docs.oracle.com/javase/tutorial/reflect/TOC.html - reflection
  +
* http://web.archive.org/web/20090226224821/http://java.sun.com/docs/books/tutorial/javabeans/introspection/index.html - introspection
  +
  +
==Decorator design pattern ==
  +
* http://javapapers.com/design-patterns/decorator-pattern/
  +
  +
==java anonymous inner class ==
  +
* http://www.beingjavaguys.com/2013/10/what-is-anonymous-class-in-java.html
  +
  +
==apache commons beanutils ==
  +
* http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#package_description - user manual for beanutils
  +
  +
==apache commons lang==
  +
*http://commons.apache.org/proper/commons-lang/userguide.html - commons lang user guide
  +
  +
==java performance ==
  +
* http://java-performance.com/
  +
== java default char encoding ==
  +
* http://javarevisited.blogspot.com/2012/01/get-set-default-character-encoding.html
  +
  +
== arrow operation, lambda ==
  +
* http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
  +
* http://vanillajava.blogspot.com/2014/09/lambdas-and-side-effects.html
  +
  +
== java8 features ==
  +
* https://www.jianshu.com/p/5b800057f2d8

Latest revision as of 09:58, 8 May 2018

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);
}
  • 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) {
        System.out.println(",");
  }
  System.out.println(numbers[i]);
}

example need iterator:

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

}


Auto Boxing/ Auto un-boxing[]

  • auto conversion between primitives and wrappers

Type safe Enums[]

  • 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) ...
    }
    ....
    Dialog.show(Dialog.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) ...
    }
...
Dialog.show(Duration.Long);

}


Varargs[]

  • 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)[]

  • attach extra information about code
    • compiler check
    • code analysis
@Override
public boolean equals(Object obj)


@Deprecated
public doSomething() ...


@SuppressWarnings("deprecation")
public static void selfDestruct() {
    Thread.currentThread().stop();
}

static import[]

  • avoiding qualifying static members

formated output[]

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

a Iterator with predicate[]

  • FilterIterator in commons cllection

https://github.com/yren/java-basic/blob/master/src/main/java/com/lokvin/basic/FilterIterator.java

java finalize method[]

java enum[]

java annotation[]

java reflection & introspection[]

Decorator design pattern[]

java anonymous inner class[]

apache commons beanutils[]

apache commons lang[]

java performance[]

java default char encoding[]

arrow operation, lambda[]

java8 features[]