push and pull

difference with email as example https://motorola-global-eng.custhelp.com/app/answers/detail/a_id/15859/~/what-is-the-difference-between-push-and-pull-email%3F

sns: server push message to client whenever the message is available

sqs: client pull messages from server; server sends out the message only when the client requests.

interesting topic: implement reliable push mechanism



Bascially, you check if an object is an instance of a specific class. You normally use it, when you have a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type (normally more concrete).


public void doSomething(Number param) {
  if( param instanceof Double) {
    System.out.println("param is a Double");
  else if( param instanceof Integer) {
    System.out.println("param is an Integer");

  if( param instanceof Comparable) {
    //subclasses of Number like Double etc. implement Comparable
    //other subclasses might not -> you could pass Number instances that don't implement that interface
    System.out.println("param is comparable"); 

Note that if you have to use that operator very often it is generally a hint that your design has some flaws. So in a well designed application you should have to use that operator as little as possible (of course there are exceptions to that general rule).

string buffer


String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable – it cannot change its internal state.

On the other hand StringBuilder is mutable. When you call append(..) it alters the internal char array, rather than creating a new string object.

Thus it is more efficient to have:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500; i ++) {

rather than str += i, which would create 500 new string objects.

an example to implement Iterator

Just implement iterator() { return MydefinedIteratorInstance };
Look at http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

public class ArrayList implements Iterable<E> {
 public int size();
 public Iterator<E> iterator() { return new Itr(this); }
  private class Itr implements Iterator<E> { 
         int cursor = 0;
         ArrayList my_array;
         public Itr(ArrayList al) {
              this.my_array = al;
         public boolean hasNext() {
             cursor != size(); // size() is defined in outer class
IF there is parameter that needed to pass to Iterator, change return a iterator with parameter constructor: Iterator(parameter)
  • A main method that instantiates a DataStructure object (ds) and uses it to fill the arrayOfInts array with integer values (0, 1, 2, 3, and so on), then calls a printEven method to print out values of even indices of arrayOfInts.
public class DataStructure {
    // create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        // fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;

    public void printEven() {
        // print out values of even indices of the array
        InnerEvenIterator iterator = this.new InnerEvenIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");

    // inner class implements the Iterator pattern
    private class InnerEvenIterator {
        // start stepping through the array from the beginning
        private int next = 0;

        public boolean hasNext() {
            // check if a current element is the last in the array
            return (next <= SIZE - 1);

        public int getNext() {
            // record a value of an even index of the array
            int retValue = arrayOfInts[next];
            //get the next even element
            next += 2;
            return retValue;

    public static void main(String s[]) {
        // fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:


For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object’s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
    class InnerClass {