Thursday, December 19, 2013

Use package members in Java

Summarize what is at http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html to make it simple.

Referring to a Package Member by Its Qualified Name
eg: 
   graphics.Rectangle myRect = new graphics.Rectangle();

Importing a Package Member
eg: 
   import graphics.Rectangle;
   Rectangle myRectangle = new Rectangle();

Importing an Entire Package
eg: 
   import graphics.*;
   Rectangle myRectangle = new Rectangle();

Apparent Hierarchies of Packages
At first, packages appear to be hierarchical, but they are not.
eg:
  java.awt package,
  java.awt.color package,
  java.awt.font package,
   and many others that begin with java.awt.xxxx

However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package.

The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.


Name Ambiguities
Have to use the member's fully qualified name to indicate which you want.
eg: 
    pkgA.Rectangle rect1;
    pkgB.Rectangle rect2;

The Static Import Statement
Wen to access to static final fields (constants) and static methods from classes, the static import statement gives you a way to import the constants and static methods that you want to use so that you do not need to prefix the name of their class. In short, it's to make static stuff of a class available, it's kind of away from package include we are talking about above.
eg:
   import static java.lang.Math.PI;   //Math is a class & we use its static stuff.
       or as a group:
   import static java.lang.Math.*;

   double r = cos(PI * theta);     // cos() and PI are static stuff of class Math. 




how JDBC works


Sunday, December 15, 2013

static keyword

Basic information about static: 
1. In general, the static keyword means that the entity to which it is applied is available outside any particular instance of the class in which the entity is declared.
2. A static field (member variable of a class) exists once across all instances of the class.
3. A static method may be called from outside the class without first instantiating the class. Such a reference always includes the class name as a qualifier of the method call.
In java, MyClass.f1()
In c++, MyClass::f1()

Here is the understanding about what static means from another angle:
It's used to create utility tools that are ONLY related to the class to which they're applied to. Basically it's a relationship between these static member functions/utility tools and the class.
In theory, you can create these static member variable / member functions anywhere outside the class. However, since those member functions/variables that are declared as static have no use to other components, so it's better to declare them within the class to which they are useful.

One of advantages to doing so is that these static member functions/utility tools have access to the private/protected member variables via entities of the class. Pay attention to "via entities of the class", the reason is these static member functions can only access static member variables. However, they can access private/protected member variables via entities of the class. In contrast, utility tools that are declared outside the class can't.

For example in c++,

static void MyClass::statc_utility_func(MyClass a)
{
  // access private member variable via entity of class. 
   cout << a.data << endl;   // assuming a.data is a private member variable .
}

void outside_utility_func(MyClass a)
{
   cout << a.data << endl;     //  It will be a compile error 'cause it can't access private members.
}

Wednesday, December 11, 2013

vtable : virtual method table

Words from wikipedia about virtual method table: 

A virtual method table, virtual function table, virtual call table, dispatch table, or vtable, is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).

Suppose a program contains several classes in an inheritance hierarchy: a superclass, Cat, and two subclasses, HouseCat and Lion. Class Cat defines a virtual function named speak, so its subclasses may provide an appropriate implementation (e.g. either meow or roar).

When the program calls the speak method on a Cat pointer (which can point to a Cat class, or any subclass of Cat), the calling code must be able to determine which implementation to call, depending on the actual type of object that is pointed to. Because the type of object pointed to by the Cat pointer is not determined at compile-time, the decision as to which branch to take cannot be decided at compile-time.

There are a variety of different ways to implement such dynamic dispatch, but the vtable (virtual table) solution is especially common among C++ and related languages (such as D and C#).

Here is a graphic description about what this means.



Tuesday, December 10, 2013

lambda functions in python

Lambda functions are kind of anonymous functions. It creates functions on the fly. Instead of defining an entire method for the sole purpose of using it once, you can simply define the function right in the parameter list.
Syntax: 
    lambda [parameter_list] : expression

Two notes I want to emphasize: 
1. Function must return something, which is expression highlighted. It is similar to a regular function that returns.
2. By bracketing the whole thing, you can use it as normal function name. See examples below.

For example:
# A regular function
def incrementor(x):
  return x + 1    

print incrementor(3)
print (lambda x: x+1)(3)     #note 1 in red, note 2 is bold

#define a function sayhello
sayhello = lambda x: 'hello ' + x
print (lambda x: 'hello ' + x)('world')
print sayhello('world')

ls=['232', '32e23', '23423', '2de2']
#define a function f1
f1=lambda x: x.find('e') > -1
print filter(lambda x: (x.find('e') > -1 ), ls)
print filter(f1, ls)

Monday, December 9, 2013

A simple version of linked list in Python

It's quite simple to convert java code to Python code. Basically it's done by removing declaration of objects/variables/functions and keyword new
But there are two things you need to pay attention to when you do the conversion from java to Python.
1. null in java is replaced by None in Python.
2. Indentation in python. I debugged the python code for a while after converting it from a java code, which is posted in another post,  and found out it's due to a simple indentation issue.

Here goes the code:
#!/bin/python

class Node:
  pass

class LinkList:
  def __init__(self):
    self.head = None

  def isEmpty(self):
    return head == None

  def insert(self, a):
    node = Node()
    node.data = a
    node.next = None
    if self.head is None :
      self.head = node
    else:
      cursor = self.head
      last = self.head
      while (cursor is not None):
        if cursor.next is None:
          last = cursor
        cursor = cursor.next
      last.next = node

  def top(self):
    self.head = self.head.next

  def printList(self):
    cursor = self.head
    print  "List:  "
    while (cursor is not None):
      print cursor.data , " "
      cursor = cursor.next
    print

list = LinkList()
list.insert(1)
list.insert(2)
list.insert(3)
list.printList()
list.top()
list.printList()
list.top()
list.printList()

Sunday, December 8, 2013

multiple threads

1. Basic info about thread:
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.




  • All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM.
  • In case two threads have the same priority a FIFO ordering is followed.


  • What I try to say is about word concurrently. When there are multiple threads running concurrently, but at any given single moment, there is only one single thread running. If the threads all have the same priority, then JVM will take turns to run each of them in a super fast fashion, which tricks human being to think they are all running on the same time. In fact, it's like each of them runs for a super short time, then pauses for a few super short times, and then runs again for a super short time, then pauses for a few super short times, and so on.

    2. Basic information about synchronized keyword:
    The synchronized keyword may be applied to a method or statement block and provides protection for critical sections that should only be executed by one thread at a time, which is the one I highlighted above as "any given single moment. 

    Basically what synchronized means is simply to say "I lock something and only I can modify it, once I'm done with it then it's unlocked and somebody else can modify it. "

    Here is a piece of code that has two threads to increment the information 5000 times. Basically one thread locks the information and increments it 5000 times so it increments it from 1 to 5000 in a try,after it's done it releases the information and another thread does the same.
    Without using synchronized keyword to lock the information, two threads will take turn to increment the information, which is not what I want.

    • When applied to a static method, the entire class is locked while the method is being executed by one thread.
    • When applied to an instance method, the instance is locked while being accessed by one thread
    • When applied to an object or array, the object or array is locked while the associated code block is executed by one thread

    Here goes the code:
    public abstract class Multithread {
        private static Integer info = new Integer(0);
        static Thread t1 = new Thread() {
            public void run() {
                synchronized (info) {
                    for (int i = 0; i <= 5000; ++i) {
                        System.out.println("world " + info++ + " "
                                + this.getPriority());
                    }
                }
            }
        };
        static Runnable t2 = new Runnable() {
            public void run() {
                synchronized (info) {
                    for (int i = 0; i <= 5000; ++i) {
                        System.out.println("hello " + info++ + "  "
                                + Thread.currentThread().getPriority());
                    }
                }
            }
        };

        public static void main(String[] args) throws InterruptedException {
            t1.start();
            new Thread(t2).start();
        }
    }


    Output will be like: 
    world 1       5
    ....
    world 4996 5
    world 4997 5
    world 4998 5
    world 4999 5
    world 5000 5       // thread 1 is done with the information and release it.
    hello 5001  5
            // thread 2 picks up the information and increments it.
    hello 5002  5
    ...
    hello 10001 5

    Saturday, December 7, 2013

    which one is bigger: base or derived???

    Let me ask you a question: between an object of a base class and an object of a child class that is derived from the base class, which one is bigger in terms of information?

    Answer is : derived class object. The reason is derived class object contains base class object. It's like a child gets all his/her parents' genes,but at meanwhile he/she gets his/her own stuff.













    Friday, December 6, 2013

    a simple circular linked list in Java















    class Node {
        public int data;
        public Node next;
    }

    class LinkList {
        private Node head;
        // LinkList constructor
        public LinkList() {        head = null;    }

        // Returns true if list is empty
        public boolean isEmpty() {        return head == null;    }

        // Inserts a new Node at the head of the list
        public void insert(int a) {
            Node node = new Node();
            node.data = a;
            node.next = null;
           
            if (head == null)
            {
                head = node;
                node.next = node;  // a snake bites its tail.
            }   
            else
            {
                Node last = getLastNode();
                // insert node at the end of list.
                last.next = node;
                node.next = head; // bite head again.
            }
        }

        // tops the Node at the head of the list
        public void top() {   
            if (!isEmpty())
            {
                Node last = getLastNode();
                if (head == last)
                    head=null;
                else
                {
                    head = head.next;   
                    last.next = head;
                }   
            }
            else
                System.out.println("empty!!!");
        }

        private Node getLastNode()
        {
            // Find the last node.
            Node cursor = head;
            Node last = head;
            while (cursor != null)
            {
                if (cursor.next == head) { last = cursor; break;}
                cursor = cursor.next;
            }
           
            return last;
        }
       
        // Prints list data
        public void printList() {
            Node cursor = head;
            System.out.print("List:  ");
            while (cursor != null) {
                System.out.print(cursor.data + " ");
                cursor = cursor.next;
                if (cursor == head) break;
            }

            System.out.println("");
        }

        public static void main(String[] args) {
            LinkList list = new LinkList();
            list.insert(1);
            list.insert(2);
            list.insert(3);
           
            list.printList();
            list.top();
            list.top();
            list.top();
            list.top();
            list.printList();
            list.insert(3);
            list.insert(2);
            list.insert(3);
            list.printList();
        }
    }

    a simple double linked list in Java






    class Node {
        public int data;
        public Node prev;
        public Node next;
    }

    class LinkList {
        private Node head;

        // LinkList constructor
        public LinkList() {
            head = null;
        }

        // Returns true if list is empty
        public boolean isEmpty() {
            return head == null;
        }

        // Inserts a new Node at the head of the list
        public void insert(int a) {
            Node node = new Node();
            node.data = a;
            node.prev = null;
            node.next = null;
           
            if (head == null)
                head = node;
            else
            {
                // All below stuff is to find the last node. 
                Node cursor=head;
                Node last = head;
                while(cursor != null)
                {
                    if (cursor.next == null) last = cursor;
                    cursor = cursor.next;
                }

                // Now insert new node at the end of list.
                last.next = node;
                node.prev = last;
            }
        }

        // Top the node at the head of the list
        public void top() {
            head = head.next;
            head.prev = null;
        }

        // Prints list data
        public void printList() {
            Node cursor = head;
            System.out.print("List:  ");
            while (cursor != null) {
                System.out.print(cursor.data + " ");
                cursor = cursor.next;
            }
            System.out.println("");
        }
       
        public void printListBackwards() {
            Node cursor=head;
            Node last = head;
            while(cursor != null)
            {
                // Find the last node.
                if (cursor.next == null) last = cursor;
                cursor = cursor.next;
            }
           
            cursor = last;
            System.out.print("List:  ");
            while (cursor != null) {
                System.out.print(cursor.data + " ");
                cursor = cursor.prev;
            }
            System.out.println("");
        }
       
        public static void main(String[] args) {
            LinkList list = new LinkList();
            list.insert(1);
            list.insert(2);
            list.insert(3);
           
            list.printList();
            list.printListBackwards();
            list.top();
            list.printList();
            list.printListBackwards();
        }
    }

    a simple linked list in java



    class Node {
        public int data;
        public Node next;
    }

    class LinkList {
        private Node head;
        // LinkList constructor
        public LinkList() {        head = null;    }

        // Returns true if list is empty
        public boolean isEmpty() {        return head == null;    }

        // Inserts a new Node at the head of the list
        public void insert(int a) {
            Node node = new Node();
            node.data = a;
            node.next = null;
          
            if (head == null)
                head = node;
            else
            {
               // All stuff below is to find the last node. 
                Node cursor = head;
                Node last = head;
                while (cursor != null)
                {
                    // Find the last node.
                    if (cursor.next == null)
                        last = cursor;
                    cursor = cursor.next;
                }

                // Now insert node at the end of list.
                last.next = node;
            }
        }

        // Top the node at the head of the list
        public void top() {        head = head.next;    }

        // Prints list data
        public void printList() {
            Node cursor = head;
            System.out.print("List:  ");
            while (cursor != null) {
                System.out.print(cursor.data + " ");
                cursor = cursor.next;
            }

            System.out.println("");
        }

        public static void main(String[] args) {
            LinkList list = new LinkList();
            list.insert(1);
            list.insert(2);
            list.insert(3);
          
            list.printList();
            list.top();
            list.top();
            list.printList();
        }
    }

    java inner class

    1. Inner class are helper classes that can access member variables of  something outer (class, function, blocks and expression ) and are for local use only. They are no need to be aware of by outside world. 

    2. Three types of inner classes:
    inner class: inside a class
    local class: inside any block ( expression, statement or block )
    anonymous class: are expressions

    Not matter how you categorize them, basically they are local to something outer.

    I want to talk a little more about anonymous class. Basically it's 2-in-1: implementation + initialization

        interface HelloWorld {
            public void greet();
            public void greetSomeone(String someone);
        }

        HelloWorld frenchGreeting = new HelloWorld() {
            String name = "tout le monde";
            public void greet() {
                greetSomeone("tout le monde");
            }
            public void greetSomeone(String someone) {
               name = someone;
               System.out.println("Salut " + name);
            }
        };
     
    It's the same as 
     class FrenchGreeting implements HelloWorld { same blah above...}
     FrenchGreeting frenchGreeting = new FrenchGreeting();
     
    However, by doing 2-in-1, you not only get rid of creating 
    new class name FrenchGreeting, which is why it's called anonymous 
    class,  but also initialize an object of it. So it's much simpler. 

    java primitive type

    Data Type Default Value (for fields)
    byte  8-bit signed integer 0
    short  16-bit signed integer 0
    int  32-bit signed integer 0
    long 64-bit singed integer 0L
    float 32-bit floating point 0.0f
    double 64-bit floating point 0.0d
    char 16-bit Unicode character '\u0000'
    String (or any object)   null
    boolean  two possible values false




    Literal: To describe something as literal is to say that it is exactly what it seems to be.


    Literals  ( = contants )

    You may have noticed that the new keyword isn't used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it's possible to assign a literal to a variable of a primitive type:
    boolean result = true;
    char capitalC = 'C';
    byte b = 100;
    short s = 10000;
    int i = 100000;
    


    Monday, December 2, 2013

    find with -exec or xargs

    The most basic thing about  find combined with -exec or xargs.


    Basically, find finds a list of something and sends them to a command and let
    the command to deal with them. The point I'm trying to make here is to show you that when find sends these something to a command, it can send them one at a time or all of them at a time. When it does one at a time, it's less efficient but it's safer and more flexible. In the opposite side, when it sends something all at a time, it's more efficient but less flexible.

    Say you have three files: a1,  a2 and a3


    #1.  \; sends parameters to the command following -exec one at a time.
    $ find . -type f -name "a*" -exec echo '{}' \;
    ./a1   -- '{}' represents each of one of them each time echo runs.
    ./a2
    ./a3


    #2.  Both + in -exec and xargs: collects the filenames into groups or sets, and runs the command once per set
    $ find . -type f -name "a*" -exec echo '{}' +
    ./a1 ./a2 ./a3
    $ find . -type f -name "a*" | xargs     
    ./a1 ./a2 ./a3

    Summary: 
    1. -exec normally sends parameters found to command line one at a time with \; at end
    2. -exec can also combine parameters into one line and send it as a whole to command line with + at end, similar to xargs does.
    3. We can add extra parameters to command line when using -exec with \;. However, you are not able to do so using -exec using +, same goes to using xargs.The combined one line parameter is all you got to send to command line when using xargs or -exec with +.
    4. -exec with +   is kind of equal to   xargs

    For example, ~/ is an extra parameter.
    find . -type f -exec cp '{}'  ~/  \;    
    find . -type f -exec cp '{}'  ~/ +      # not working
    find . -type f  | xargs cp ~/              # not working

    Useful tips:
    $ find . -type f   -mmin  -30   # files modified less than 30mins ago.  
    $ find . -type f   -mtime  -3    # files modified less than 3 days ago.

    Saturday, November 30, 2013

    taker


    #include <iostream>
    #include <iomanip>
    //#include <sstream>
    //#include <string>
    //#include <vector>

    using namespace std;

    class ABC {
    public:
        ABC() {
        }

        void operator()(int a) {
            cout << "a is  " << a << endl;
        }

        void set(const int& a) {
            this->a = a;
        }

        void set2(const int& b) {
            this->b = b;
        }

        static int get_sta() {
            return 10;
        }

        int& get()
        {
            return a;
        }

        int& get2()
        {
            return b;
        }

    private:
        int a;
        int b;
    };

    template<typename R, typename C>
    class taker {
    public:
        typedef void (C::*SetFuncType)(const R&);
        typedef R& (C::*GetFuncType)();

        taker(C& anAssignee, SetFuncType aSetFunction, GetFuncType aGetFunction) :
            theAssignee(anAssignee), theSetFunction(aSetFunction),
                    theGetFunction(aGetFunction) {
        }

        // use = to set its value.
        taker<R, C>& operator=(R aValue) {
            (theAssignee.*theSetFunction)(aValue);
            return *this;
        }

        // implicitly cast object to Return type : R
        operator R() const {
            return (theAssignee.*theGetFunction)();
        }

    private:
        C& theAssignee;
        SetFuncType theSetFunction;
        GetFuncType theGetFunction;
    };

    // exposer : it can return a reference to object taken in from constructor.
    template<typename R, typename C>
    class exposer
    {
    public:
        typedef void (C::*SetFuncType)(const R&);
        typedef R& (C::*GetFuncType)();

        exposer(C& anObject, SetFuncType aSetFunction, GetFuncType aGetFunction) :
            theAssignee(anObject), theSetFunction(aSetFunction),
                    theGetFunction(aGetFunction)
        {
        }

        // Return a reference to the object taken in from constructor,
        // That's why I call it exposer.
        C& me()  
        {
            return theAssignee;
        }

        R& getValue()
        {
            return (theAssignee.*theGetFunction)();
        }

        void giveMeGet(GetFuncType aGetFunction)
        {
            theGetFunction = aGetFunction;
        }

    private:
        C& theAssignee;
        SetFuncType theSetFunction;
        GetFuncType theGetFunction;

    };

    // send non-static member function to f()
    void f(ABC& a, int& (ABC::*getFunc)()) {
        cout << (a.*getFunc)() << endl;
    }

    // send non-static member function to f_gen() : generic
    template<typename R, typename C>
    void f_gen(C& a, R& (C::*getFunc)()) {
        cout << (a.*getFunc)() << endl;
    }

    // send static member function to f()
    void f_sta(int(*pmf)()) {
        cout << (*pmf)() << endl;
    }

    int main() {

        cout << "-------------"  << endl;

        ABC abc1;
        f(abc1, &ABC::get);
        f_gen(abc1, &ABC::get);
        f_sta(&ABC::get_sta);

        taker<int, ABC> tt(abc1, &ABC::set, &ABC::get);
        tt=100;
        cout << (int)tt << endl;

        exposer<int, ABC> exp(abc1, &ABC::set, &ABC::get);
        ABC& abc2 = exp.me();
        abc2.set(22);
        cout << abc2.get() << endl;
        cout << exp.getValue() << endl;

        exp.giveMeGet(&ABC::get2);
        abc2.set2(111);
        cout << exp.getValue() << endl;


        return 0;
    }


    Monday, November 25, 2013

    Unnamed function parameters

    There could be unnamed parameters in a function, for example, void f(int a, int);  The second parameter is an unnamed parameter.

    Basically the reason you need them is to use them as place holder for future.

    Even though an unnamed parameter doesn't have a name, however, when you call the function you need to specify a value for it, the same normal way when you are dealing with normal named parameters.There is nothing special about them, except that you don't do anything with them inside the function because they don't have names and you are not able to interact with something that doesn't have a name.

    In some cases, for example post increment/decrement operator, even you don't provide values for unnamed parameters, however, compiler has already done that for you by providing the default value of whatever type that unnamed parameters belong to. So don't be fooled by that.

    In short, either you or compiler need to provide values for unnamed parameters.


    Code:
    void f(int a, int){    cout << "a is " << a << endl;}

    class ABC
    {
    public:
        ABC(int a){this->a = a;}
        int operator++(int)   {        return a++;    }
        int operator()()    {        return a;    }
    private:
        int a;
    };

     Test code:

        // You specify the value for the unnamed parameter
        f(2,3000);
        ABC a(3);
        cout << a() << endl;
        // Compiler specifies the value for the unnamed parameter,
        // which is the default int value in this case.
        cout << a++ << endl;
        cout << a() << endl;


    Result:
    a is 2
    3
    3
    4

    Friday, November 22, 2013

    Categories of template parameters

    There are three categories of template parameters, which I mentioned in another post. Here I just want to give a demo to demonstrate their uses all at 
    once.

    1) Type: that are preceded by the class or typename keywords , 
        which represent types. 
        eg:   class T
    
    2) Non-Type : regular typed parameters, similar to those found in functions.
        eg:    int N
    
    3) Template :  that are preceded by the template keywords
       which represent templates.   
        eg:    template <class U> class V
                         
    

    Example:

    // Notice T in red is used to tell what kind of type template U is taking.
    // Otherwise, I need to hard-code it as something inside the class. 
    // 1) C is type.
    // 2) N is non-type parameter,
    // 3) U is template
    template <class C, int N, class T, template<class anySymbol> class U>
    class takeThree
    {
    public:
            takeThree(C a1, U<T> b1):a(a1), b(b1), size(N) {}
            void print() const { a.print(); cout << "size = " << size <<endl; b.print();cout << endl;}
    private:
            C a;
            U<T> b;  
            int size;
    };



    Test code: 

        anOrdinaryClass a(3);
        aTemplateClass<float> b(3.2);
        takeThree<anOrdinaryClass, 100, float, aTemplateClass> take_three(a, b);
        takeThree<aTemplateClass<float>, 200, float, aTemplateClass> take_three2(b, b);
        take_three.print();
        take_three2.print();

    Result: 
    ord data is 3
    size = 100
    temp data is 3.2

    temp data is 3.2
    size = 200
    temp data is 3.2

    Support classes listed down here :


    class anOrdinaryClass
    {
    public:
            anOrdinaryClass(int b):a(b){}
            void print() const      {cout << "ord data is " << a <<endl;}
    private:       int a;
    };

    template <class T>
    class aTemplateClass
    {
    public:
            aTemplateClass(T b){a=b;}
            void print() const      {cout << "temp data is " << a <<endl;}
    private:        T a;
    };

    Thursday, November 21, 2013

    Does int*& scare you ?

    When you see a function declaration like this "void f(int*&); ", does it
    scare you a little?  Pass-by-reference seems to be an unique feature in C++ program language, not in c, not in java, not in python. What it means is like
    someone walks into a function itself, then whatever you do to that guy inside the function has effects to that guy because it's THAT guy in the function, not somebody else.

    Now let's back to int*& in "void f(int*&);",  what it means is a variable( let's say its name is ip) , whose value is a memory address, walks into f() itself. 

    Normally we can change the content in that address via *ip.
        For example, *ip  = 4;

    But this time, we can also change its value by assigning another address to it.
        For example,  ip = new node();

    After you've done that, ip refers to a totally different memory location, not the previous memory address anymore.

    In short, if you pass a "address type", which is T*,  variable into a function by reference, then you can assign a new address to that variable inside the function. After the function is gone, that variable now refers to a new address. It's just simple like that. 

    Example:

    struct node
    {
        int a;
        node* next;
    };

    typedef node* ADDR;

    // void f(ADDR& change, ADDR noChange)    // not that scaring.
    void f(node*& change, node* noChange)       // a little scaring
    {
        // Create an address tmp.
        ADDR tmp = new node();
        tmp->a = 100;
        tmp->next = NULL;

        // Assign the new address to both change and neverChange
        // Now variable "change" that comes into f() itself is assigned with a new address.
        change = tmp;
        // Local variable "noChange", NOT variable "noChange" coming into f(),  is assigned with a new address.
        noChange = tmp;

        // Normally we modify the contents of the address.
        // but this time I don't do it.
        // *change = *tmp;
        // *noChange = *tmp;
    }


    Test code: 

            ADDR change = new node();   
            ADDR noChange = new node();
       
            change->a = 10;
            change->next = NULL;

            noChange->a = 10;
            noChange->next = NULL;

            cout << "Addresses are " << change  << "   "  << noChange << endl;
            cout << "Value are " << change->a  << "   "  << noChange->a << endl;
            f(change, noChange);
            cout << "Addresses are " << change  << "   "  << noChange << endl;
            cout << "Value are " << change->a  << "   "  << noChange->a << endl;

    Result: 

    Addresses are 0x79c320   0x79c340
    Value are 10   10
    Addresses are 0x79c360   0x79c340
    Value are 100   10


    Me, me, me

    Me me me: it's always me. 

    Overwriting operator() to return an object itself as reference does two things:
    1. make a class as a functor, so you can do something like f().
    2. Return a reference to itself, which is why I call it "me me me" .


    Example: 

    class Me
    {
    public:
        Me(int a):theA(a){}
        Me& operator()(int a){theA+=a; return *this;}
        friend ostream& operator<<(ostream& os, Me a)
        {
            os << "Value is " << a.theA << endl;
            return os; 
        }

    private:
        int theA;
    };

    Test code: 
            Me me(4);
            cout << me(1000)(200)(30);

    Result: 
    Value is 1234


    me(10) is the same as object me.
    me(10)(20) is the same as object me too.
    me(10)(20)(30) is the same as object me as well.

    So it's always me, me, me...... Isn't it kind of entertaining yourself while doing
    tasteless coding, is it?

    Wednesday, November 20, 2013

    T* : address type

    I'm not sure term "address type" has been used in c yet, but I rather call pointer this way. Like "integer type"/int, whose value is integer, "address type"'s value is address, in which you can store everything: int, float, address, etc. Yep, you can store an address in another address. And if you like, you can do so recursively. As a result of that, you are dealing with T*, T**, T***, ...

    When you see a struct like this, what do you see?
    I'm sure it will make you dizzy if you're c programming beginner.

    struct Node
    {
       int data;
       Node* prev;
       Node* next;
    };

    But what I see is a node with three elements in there: one int and two addresses. It's no big different from a node with three ints in there.
    struct Node
    {
       int data;
       ADDR prev;
       ADDR next;
    };


    // 1. "T*" is called "address type": its value is an address, in which
    // any ordinary type or "address type" can stay.
    typedef int* ADDR;
    typedef ADDR* ADDR2;
    //typedef int** ADDR2;

    // 2. Parameter pass to function in c is by value only.
    // f() takes in int, ADDR and ADDR2 by value (copy of values).
    // it has nothing to do with "int argument".
    // but it DOES have something to do with "ADDR argument" 
    // or "ADDR2 arguemnt" because it can play with the content
    // in that address, which, in turn, causes post-effects after f() is gone.
    void f(int a, ADDR b, ADDR2 c)
    {
        a = 3;
        // change the content, which is int, in b
        *b = 20;
        // change the content, which is address, in c
        *c = b;
    }


    int main()
    {
        int a;
        ADDR b = new int ;
        ADDR2 c = new ADDR;
        cout << "a is " << a << endl;
        cout << "b is  " << b <<  " what in b is " << *b << endl;
        cout << "c is  " << c <<  " what in c is " << *c << endl;
        f(a, b, c);
        cout << "a is " << a << endl;
        cout << "b is  " << b <<  " what in b is " << *b << endl;
        cout << "c is  " << c <<  " what in c is " << *c << endl;

        return 0;
    }


    Result:
    1. None of a, b, or c's value after f() has changed, that's what     
        pass-by-value  means.
    2. What is changed is the contents in these addresses: b and c.
        b's content (int) changed from 0 to 20.
        c's content (ADDR) changed from 0 to 0xbd46010

    a is 0
    b is  0xbd46010 what in b is 0
    c is  0xbd46030 what in c is 0
    a is 0
    b is  0xbd46010 what in b is 20     
    c is  0xbd46030 what in c is 0xbd46010

    use pointer to make arrays in a simple way

       // Define three address types.
       // We need to think "address type" as a type like any other
       // ordinary type such as "integer type ( int )"
       // So in our example, ADDR is just an ordinary type as int.
       // Also let's say "new" operator is to create an address. 
        typedef int* ADDR;        // Its value is address, where int stays
        typedef int** ADDR2;    // Its value is address, where ADDR stays
        typedef int*** ADDR3;   // Its value is address, where ADDR2 stays


    // Goal is to create 3D array: 3 x 4 x 5= 60
    // Option 2: allocate all the space at once
    //    ADDR head = new int[60];

        // 1. Assign i3d, whose type is address type ADDR3,
        //    with an address containing ADDR2. 
        ADDR3 i3d = new ADDR2[3]; 
        for (int i=0; i< 3 ; ++i)
        {
            // 2. Assign i3d[i], whose type is address type ADDR2,
            //     with an address containing ADDR 
            i3d[i] = new ADDR[4];
            for (int j=0; j< 4 ; ++j)
            {
               // 3. Assign i3d[i][j], whose type is address type ADDR,
               //     with an address containing int
                i3d[i][j] = new int[5];

             // Option 2: 
             //   i3d[i][j] = head + i * 4 * 5 + j * 5;
            }
        }


    // Use i3d like this:  i3d[i][j][k]

        for (int i=0; i< 3 ; ++i)
            for (int j=0; j< 4 ; ++j)
                for (int k=0; k < 5 ; ++k)
                    i3d[i][j][k] = i*100 + j*10 + k;


    // Release space like this in a reversed order.

        for (int i=0; i< 3 ; ++i)
        {
            for (int j=0; j< 4 ; ++j)
            {
                delete [] i3d[i][j];
            }
            delete [] i3d[i];
        }
        delete i3d;

        // Option 2: 
        // for (int i=0; i< 3 ; ++i)
        // {
        //     delete [] i3d[i];
        // }
        // delete
    [] i3d;

        // delete
    [] head; 




    Tuesday, November 19, 2013

    pointer in c

    Pointer in c is not simple to understand, but it's not that difficult either.
    So for instance, "int* ip;",  it's not a big deal.
    But how about "int** ip";   or  "int*** ip;" ? In no time, it will make you dazed and confused, sometimes it happens to even an experienced c programmer.

    What I try to do here is to explain it in a new way that hopefully can prevent you from feeling dazed when you deal with them.

    1. First, let me borrow template parameter concept in C++ here to use "T" to represent a type, which could be any type like int, int*, int**, float, etc.  Yep, consider "int*" and "int**" as type as well.

    2. Next, make an alias of "T*" , you see "T*" as a whole as a new type, which holds memory location, similar to int, which holds integer.

    typedef T* addr; 

    For example, 

    addr  =   int*   if  T = int
    addr = int**   if T = int*
    addr = double  if T = int**



    typedef int* addr;
    int a;     // a holds an integer
    addr  b; // b holds a memory address (containing int)


    ==================================
    Here is a piece of code for demo:
    ==================================

    typedef int* addr;
     
    // two memory addresses passed in by value.
    void f(int* ip, addr* ia)        // equal to    void f(int* ip, int** ia)
    {
       // By the way, once you get addresses passed in, the only thing you can do inside a function
       // is to manipulate the contents in them via *ip, for instance.

        // Modify the content in the first memory location passed in.
        // so new integer value 4 is stored in that location. 
        int a = 1000;    // int type
        *ip = a;

            addr  b = (int*)malloc(3 * sizeof(int));   // addr type
            b[0] = 11;
            b[1] = 12;
            b[2] = 13;

        // Modify the content in the second memory location passed in.
        // !!! so new addr value b ( address) is stored in that location. !!!
        // This is AMAZING !!! Reason is that we are sending out a memory location out back to 
        // calling function. For example, you allocate a chunk of dynamically allocated memory within
        // the function, calling function will have access to the chunk of memory. 
        *ia = b;
    }

    int main(void) {
        int a=3;
        addr  ia;

        // I want you to think "addr" as any ordinary type such as "int". Forget about pointers!!!!
        // I send their addresses into a function and update their contents inside the function f().  
        f(&a, &ia);

       cout << a << endl;

        cout << ia[0] << endl;
        cout << ia[1] << endl;
        cout << ia[2] << endl;

        return 0;
    }

    memory locations: Code segment + Data segment ( init + uninit + heap + stack )

    This article, which is posted at http://www.beingdeveloper.com/memory-layout-of-a-program/,
    gives a very summary of memory locations. I love the way it gets explained. It seems to be the best
    described compared to other posts I found online such as this one at
    http://www.geeksforgeeks.org/memory-layout-of-c-program/.


    A program, basically, is a sequence of instructions and a set of data which is manipulated by these instructions.

    1. #include <stdio.h>
    2. int g_i = 100; /* A global variable */
    3. int g_j; /* An uninitialized global variable */
    4. int main(void) /* A function */
    5. {
    6.     int l_i = 1; /* A local variable */
    7.     static int s_i = 2; /* A static local variable */
    8.     int c;
    9.     for (c = 0; c < 1000; c++)
    10.     {
    11.         l_i += c;
    12.     }
    13.     return 0;
    14. }


    When this code is compiled and linked, we have an executable “program”. When we execute the program, this code will be loaded into the virtual address space that the operating system allocates for it. And in this post I will talk about how the instructions and the data of the program are arranged in its virtual address space.
    Basically, the memory space for a program has two parts: the code segment which holds the program’s executable instructions and the data segment which holds data manipulated by the instructions.
    To look closely in details, a typical address space of a program looks like this:
    A Typical C Language Memory Layout

    Code Segment

    The code segment, more often called Text Segment, starts usually from the low address and contains the executable instructions (code) of the program. The text segment is static and protected from modification, which means that once loaded, the content of the text segment cannot be modified.
    In our example, the text segment contains the machine instructions corresponding to our main() function, including the initialization instruction of the local variable l_i, the initialization instruction of the loop counter c and the loop itself.

    “Data Segment”

    The “data segment” is more complicated than the code segment, and it is more “active” also. The “data segment” can be further classified into 4 sections.

    Initialized Data Segment

    The Initialized Data Segment is the portion of memory space which contains global variables and static variables that are initialized by the code. In our example, the global variable g_i and the static variable s_i are stored in the Initialized Data Segment.
    If you read carefully enough, you may have noticed that I put the title of this section in quotes. That’s because that we usually use data segment to refer to the Initialized Data Segment, I used the term “Data Segment” in the title and in the illustration to fabric a general term which says “a segment containing data”, which enclose the Initialized Data Segment, the Uninitialized Data Segment, the heap and the stack. If you are familiar with the x86 assembly language, you would probably often say “start a data section with the .DATA directive” and “allocate a stack space with the .STACK directive”. But here I used the term “Data Segment” to refer to all the sections which are used to stock data.

    Uninitialized Data Segment

    The Uninitialized Data Segment, also referred to as BSS, contains all the global variables and static variables that are NOT initialized by the programmer. In our example, the global variable g_j will be stored in the Uninitialized Data Segment.

    Stack section and Heap area

    The stack section and the heap area, face to face, occupy the rest of the virtual memory space of the program. Usually, the stack section starts from the highest address of the virtual memory space and increases towards the lower address of the virtual memory space. Contrarily, the heap area starts from the lowest address after the uninitialized data segment and increases towards the highest address of the virtual memory space.
    The stack section is used to store automatic variables (non-static local variables) and the calling environment each time a function is called. In the stack section, variable spaces are allocated dynamically by moving up and down the stack pointer which indicates the top of the stack. When a variable goes out of scope, the stack pointer simply goes up and the variable space is no longer usable. This management manner makes the memory allocation in stack very fast. I will talk about the Memory allocation and variable scope in future posts.
    The heap area is a space area often used for dynamic memory allocation and is managed by malloc, realloc and free). The allocation of the space for a new variable in the heap is usually much slower than is in the stack because the heap may contain non-contiguous regions caused by dynamic allocation and free of spaces. The heap area is shared by all threads of the program’s process. In contrary, each thread possesses its own stack section.

    Thursday, November 14, 2013

    policy class

    #include <iostream>

    using namespace std;

    template<class T>
    struct Creator_1
    {
        static T* create(){
            T* node = new T();
            *node = 40;
            return node;
        };
    };

    template<class T>
    struct Creator_2
    {
       // you don't want to do this way.  Doing so for demo purpose.
        static T* create(){
            T a=100;
            return &a;
        };
    };

    // Way 1 to declare it :

    template <class CreationPolicy>
    class CreatorManger: public CreationPolicy
    {
    };

    // Way 2 to declare:  template template class.
    template <template <class T> class CreationPolicy>
    class CreatorManger_2: public CreationPolicy<int>
    {
    };

    int main()
    {
        // Way 1 to use it : expect any type as template argument.

        CreatorManger<Creator_1<int> > manager;
        CreatorManger<Creator_2<int> > manager2;

        int* ip1 = manager.CreatorManger::create();
        int* ip2 = manager2.CreatorManger::create();

        cout << *ip1 << endl;
        cout << *ip2 << endl;


        // Way 2 to use it : expect a template class as template argument

        CreatorManger_2<Creator_1> manager3;
        CreatorManger_2<Creator_2> manager4;

        int* ip3 = manager3.CreatorManger_2::create();
        int* ip4 = manager4.CreatorManger_2::create();

        cout << *ip3 << endl;
        cout << *ip4 << endl;


        return 0;
    }

    template template class

    I posted it initially at
    http://www.cplusplus.com/forum/general/116769/

    #include <iostream>
    using namespace std;
    
    /*
     *   -------------------------------------
     *    1. template parameter  ( ordinary case )
     *   -------------------------------------
     *   template <class C>
     *   class takeAny
     *
     *   It means:
     *   takeAny only takes ordinary type, including template class,
     *   in as its template parameter, less restrictive
     *
     *   -------------------------------------
     *   -------------------------------------
     *    2. template template parameter  ( special case I'm talking about today. )
     *   -------------------------------------
     *   template <template <class T> class C>
     *   class takeTC_Only
     *
     *   It means:
     *   takeTC_Only ONLY takes "template class"
     *   in as its template parameter, more restrictive
     *
     *   -------------------------------------
     *
     *   We should consider "template <class T> class C" as a whole.
     *   "T"'s sole purpose is to indicate that "C" is a template class, which
     *  takeTC_Only expects when doing initialization.
     *
     *   In another word, we can use "C" as a type inside class takeTC_Only to declare variables.
     *   However when I do so inside class takeTC_Only, I also need to make sure that C is used as template
     *   class that must be initialized with other specific types, which is a tricky part.
     *
     *   Type "T" could NOT be used inside class takeTC_Only to declare variables.
     *   Again, its sole purpose is to indicate that "C" is a "template class" and class takeTC_Only
     *   must take a "template class" as its template class.
     *   In another word, if we feed class takeTC_Only with an ordinary class, then it will be an error
     *   because class takeTC_Only only expects a template class to get itself initialized.
     *
     */
    
    
    template <class T>
    class aTemplateClass
    {
    public:
            aTemplateClass(T b){a=b;}
            void print() const      {cout << "data is " << a <<endl;}
            T& get(){return a;}
    private:
            T a;
    };
    
    class anOrdinaryClass
    {
    public:
            anOrdinaryClass(int b){a=b;}
            void print() const      {cout << "data is " << a <<endl;}
            int& get(){return a;}
    private:
           int a;
    };
    
    // You can only feed in "a template class" as its template argument when
    // initialize takeTC_Only.
    template <template <class T> class C>
    class takeTC_Only
    {
    public:
            takeTC_Only(C<int> b):a(b) { }
            void print() const { a.print();}
    private:
         // Here is the tricky part: you need use "C" as template class.
            // You can NOT do "C<T>& a;" because it doesn't recognize "T" as template parameter.
            C<int>& a;
    };
    
    // You can either feed in "an ordianry class"  OR "a template class" as
    // its template argument when initialize takeAny.
    template <class C>
    class takeAny
    {
    public:
            takeAny(C b):a(b) { }
            void print() const { a.print();}
    private:
            C& a;
    };
    
    
    int main() {
            // ##############################################################
            // #  1. template class as template parameter : so-called template template class 
            // ###############################################################
            // takeTC_Only MUST take "a Template class" in as its template parameter.
            // "aTemplateClass" is a template class so it can be used to initialize takeTC_Only.
            aTemplateClass<int> a(3);
    
           // tricky part#2: you don't do "takeTC_Only<aTemplateClass<int> > takeTC(a); ", 
           // instead, you do  "takeTC_Only<aTemplateClass> takeTC(a);"
            takeTC_Only<aTemplateClass> takeTC(a);
            takeTC.print();
    
            // #######################################
            // #  2. any type as template parameter  
            // #######################################
            // template class takeAny takes either an Ordinary class OR a template class in as its template parameter.
            // 1. Take an ordinary class as template parameter.
            anOrdinaryClass b(3);
            takeAny<anOrdinaryClass> take_any(b);
            take_any.print();
    
            // 2. Take a template class as template parameter.
            takeAny<aTemplateClass<int> > take_any2(a);
            take_any2.print();
    
            return 0;
    }
     
     
    Three things a template class can take as template parameter: 
    1) Type: that are preceded by the class or typename keywords , which represent types. 
        eg:   class T
    
    2) Non-Type : regular typed parameters, similar to those found in functions.
        eg:    int N
    
    3) Template :  that are preceded by the template keywords , which represent templates.   
        eg:    template <class U> class V
                         
    template <class T, int N, template <class U> class V>
    class Got_All_Three 
    {
    };


    For example,
    Got_All_Three<anOrdinaryClass, 100, aTemplateClass> gat;

    or

    Got_All_Three<aTemplateClass<int>, 100, aTemplateClass> gat; 
    
    

    XML introduction

    11/14/2013, 

    I found this document long time ago and saved it.  Today I happened to cross it again and still felt that this is a really good article about XML, so I blogged it.

    Introduction to XML
    XML was designed to transport and store data.
    HTML was designed to display data.
    What You Should Already Know
    Before you continue you should have a basic understanding of the following:
    • HTML
    • JavaScript
    If you want to study these subjects first, find the tutorials on our Home page.
    What is XML?
    • XML stands for EXtensible Markup Language
    • XML is a markup language much like HTML
    • XML was designed to carry data, not to display data
    • XML tags are not predefined. You must define your own tags
    • XML is designed to be self-descriptive
    • XML is a W3C Recommendation
    The Difference Between XML and HTML
    XML is not a replacement for HTML.
    XML and HTML were designed with different goals:
    • XML was designed to transport and store data, with focus on what data is
    • HTML was designed to display data, with focus on how data looks
    HTML is about displaying information, while XML is about carrying information.
    XML Does Not DO Anything
    Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store, and transport information.
    The following example is a note to Tove, from Jani, stored as XML:
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    The note above is quite self descriptive. It has sender and receiver information, it also has a heading and a message body.
    But still, this XML document does not DO anything. It is just information wrapped in tags. Someone must write a piece of software to send, receive or display it.
    With XML You Invent Your Own Tags
    The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
    That is because the XML language has no predefined tags.
    The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.).
    XML allows the author to define his/her own tags and his/her own document structure.
    XML is Not a Replacement for HTML
    XML is a complement to HTML.
    It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used to transport data, while HTML is used to format and display the data.
    My best description of XML is this:
    XML is a software- and hardware-independent tool for carrying information.
    XML is a W3C Recommendation
    XML became a W3C Recommendation February 10, 1998.
    To read more about the XML activities at W3C, please read our W3C Tutorial.
    XML is Everywhere
    XML is now as important for the Web as HTML was to the foundation of the Web.
    XML is the most common tool for data transmissions between all sorts of applications.
    How Can XML be Used?
    XML is used in many aspects of web development, often to simplify data storage and sharing.
    XML Separates Data from HTML
    If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.
    With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for layout and display, and be sure that changes in the underlying data will not require any changes to the HTML.
    With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.
    XML Simplifies Data Sharing
    In the real world, computer systems and databases contain data in incompatible formats.
    XML data is stored in plain text format. This provides a software- and hardware-independent way of storing data.
    This makes it much easier to create data that can be shared by different applications.
    XML Simplifies Data Transport
    One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet.
    Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.
    XML Simplifies Platform Changes
    Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of data must be converted and incompatible data is often lost.
    XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.
    XML Makes Your Data More Available
    Different applications can access your data, not only in HTML pages, but also from XML data sources.
    With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc), and make it more available for blind people, or people with other disabilities.
    XML is Used to Create New Internet Languages
    A lot of new Internet languages are created with XML.
    Here are some examples:
    • XHTML 
    • WSDL for describing available web services
    • WAP and WML as markup languages for handheld devices
    • RSS languages for news feeds
    • RDF and OWL for describing resources and ontology
    • SMIL for describing multimedia for the web 
    If Developers Have Sense
    If they DO have sense, future applications will exchange their data in XML.
    The future might give us word processors, spreadsheet applications and databases that can read each other's data in XML format, without any conversion utilities in between.
    XML Tree
    XML documents form a tree structure that starts at "the root" and branches to "the leaves".
    An Example XML Document
    XML documents use a self-describing and simple syntax:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).
    The next line describes the root element of the document (like saying: "this document is a note"):
    <note>
    The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    And finally the last line defines the end of the root element:
    </note>
    You can assume, from this example, that the XML document contains a note to Tove from Jani.
    Don't you agree that XML is pretty self-descriptive?
    XML Documents Form a Tree Structure
    XML documents must contain a root element. This element is "the parent" of all other elements.
    The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
    All elements can have sub elements (child elements):
    <root>
      <child>
        <subchild>.....</subchild>
      </child>
    </root>
    The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters).
    All elements can have text content and attributes (just like in HTML).
    Example:
    The image above represents one book in the XML below:
    <bookstore>
      <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
      </book>
      <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>
    </bookstore>
    The root element in the example is <bookstore>. All <book> elements in the document are contained within <bookstore>.
    The <book> element has 4 children: <title>,< author>, <year>, <price>.
    XML Syntax Rules
    The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use.
    All XML Elements Must Have a Closing Tag
    In HTML, elements do not have to have a closing tag:
    <p>This is a paragraph
    <p>This is another paragraph
    In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
    <p>This is a paragraph</p>
    <p>This is another paragraph</p>
    Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.
    XML Tags are Case Sensitive
    XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
    Opening and closing tags must be written with the same case:
    <Message>This is incorrect</message>
    <message>This is correct</message>
    Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.
    XML Elements Must be Properly Nested
    In HTML, you might see improperly nested elements:
    <b><i>This text is bold and italic</b></i>
    In XML, all elements must be properly nested within each other:
    <b><i>This text is bold and italic</i></b>
    In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.
    XML Documents Must Have a Root Element
    XML documents must contain one element that is the parent of all other elements. This element is called the root element.
    <root>
      <child>
        <subchild>.....</subchild>
      </child>
    </root>

    XML Attribute Values Must be Quoted
    XML elements can have attributes in name/value pairs just like in HTML.
    In XML, the attribute values must always be quoted.
    Study the two XML documents below. The first one is incorrect, the second is correct:
    <note date=12/11/2007>           //wrong
      <to>Tove</to>
      <from>Jani</from>
    </note>

    <note date="12/11/2007">       //right
      <to>Tove</to>
      <from>Jani</from>
    </note>
    The error in the first document is that the date attribute in the note element is not quoted.
    Entity References
    Some characters have a special meaning in XML.
    If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.
    This will generate an XML error:
    <message>if salary < 1000 then</message>
    To avoid this error, replace the "<" character with an entity reference:
    <message>if salary &lt; 1000 then</message>
    There are 5 predefined entity references in XML:
    &lt;
    less than
    &gt;
    greater than
    &amp;
    &
    ampersand 
    &apos;
    '
    apostrophe
    &quot;
    "
    quotation mark
    Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.
    Comments in XML
    The syntax for writing comments in XML is similar to that of HTML.
    <!-- This is a comment -->
    White-space is Preserved in XML
    HTML truncates multiple white-space characters to one single white-space:
    HTML:
    Hello           Tove
    Output:
    Hello Tove
    With XML, the white-space in a document is not truncated.
    XML Stores New Line as LF
    In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). In Unix applications, a new line is normally stored as an LF character. Macintosh applications also use an LF to store a new line.
    XML stores a new line as LF.
    XML Elements
    An XML document contains XML Elements.
    What is an XML Element?
    An XML element is everything from (including) the element's start tag to (including) the element's end tag.
    An element can contain other elements, simple text or a mixture of both. Elements can also have attributes.
    <bookstore>
      <book category="CHILDREN">
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
      </book>
      <book category="WEB">
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
      </book>
    </bookstore>
    In the example above, <bookstore> and <book> have element contents, because they contain other elements. <author> has text content because it contains text.
    In the example above only <book> has an attribute (category="CHILDREN").

    XML Naming Rules
    XML elements must follow these naming rules:
    • Names can contain letters, numbers, and other characters
    • Names cannot start with a number or punctuation character
    • Names cannot start with the letters xml (or XML, or Xml, etc)
    • Names cannot contain spaces
    Any name can be used, no words are reserved.

    Best Naming Practices
    Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.
    Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.
    Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.
    Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."
    Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).
    XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.
    Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.

    XML Elements are Extensible
    XML elements can be extended to carry more information.
    Look at the following XML example:
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <body>Don't forget me this weekend!</body>
    </note>
    Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:
    MESSAGE
    To: Tove
    From: Jani
    Don't forget me this weekend!
    Imagine that the author of the XML document added some extra information to it:
    <note>
    <date>2008-01-10</date>   // dxu: this is what being Extensible refers to.
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    Should the application break or crash?
    No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.
    One of the beauties of XML, is that it can be extended without breaking applications.
    XML Attributes
    XML elements can have attributes, just like HTML.
    Attributes provide additional information about an element.
    XML Attributes
    In HTML, attributes provide additional information about elements:
    <img src="computer.gif">
    <a href="demo.asp">
    Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but can be important to the software that wants to manipulate the element:
    <file type="gif">computer.gif</file>

    XML Attributes Must be Quoted
    Attribute values must always be quoted. Either single or double quotes can be used. For a person's sex, the person element can be written like this:
    <person sex="female">
    or like this:
    <person sex='female'>
    If the attribute value itself contains double quotes you can use single quotes, like in this example:
    <gangster name='George "Shotgun" Ziegler'>
    or you can use character entities:
    <gangster name="George &quot;Shotgun&quot; Ziegler">

    XML Elements vs. Attributes
    Take a look at these examples:
    <person sex="female">
      <firstname>Anna</firstname>
      <lastname>Smith</lastname>
    </person>

    <person>
      <sex>female</sex>
      <firstname>Anna</firstname>
      <lastname>Smith</lastname>
    </person>
    In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.
    There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.

    My Favorite Way
    The following three XML documents contain exactly the same information:
    A date attribute is used in the first example:
    <note date="10/01/2008">
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    A date element is used in the second example:
    <note>
      <date>10/01/2008</date>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    An expanded date element is used in the third: (THIS IS MY FAVORITE):
    <note>
      <date>
        <day>10</day>
        <month>01</month>
        <year>2008</year>
      </date>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>



    Avoid XML Attributes?
    Some of the problems with using attributes are:
    • attributes cannot contain multiple values (elements can)
    • attributes cannot contain tree structures (elements can)
    • attributes are not easily expandable (for future changes)
    Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.
    Don't end up like this:
    <note day="10" month="01" year="2008"
    to="Tove" from="Jani" heading="Reminder"
    body="Don't forget me this weekend!"
    >
    </note>

    XML Attributes for Metadata
    Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this:
    <messages>
      <note id="501">
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
      </note>
      <note id="502">
        <to>Jani</to>
        <from>Tove</from>
        <heading>Re: Reminder</heading>
        <body>I will not</body>
      </note>
    </messages>
    The id attributes above are for identifying the different notes. It is not a part of the note itself.
    What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.

    XML Validation


    XML with correct syntax is "Well Formed" XML.
    XML validated against a DTD is "Valid" XML.
    Well Formed XML Documents
    A "Well Formed" XML document has correct XML syntax.
    The syntax rules were described in the previous chapters:
    • XML documents must have a root element
    • XML elements must have a closing tag
    • XML tags are case sensitive
    • XML elements must be properly nested
    • XML attribute values must be quoted
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>

    Valid XML Documents
    A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE note SYSTEM "Note.dtd"><note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the file is shown in the paragraph below.
    XML DTD   //  Define XML document structure  --  method 1
    The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements:
    <!DOCTYPE note
    [
    <!ELEMENT note (to,from,heading,body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>
    ]>
    If you want to study DTD, you will find our DTD tutorial on our homepage.
    XML Schema      //  Define XML document structure  -- method 2
    W3C supports an XML-based alternative to DTD, called XML Schema:
    <xs:element name="note">

    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>

    </xs:element>
    If you want to study XML Schema, you will find our Schema tutorial on our homepage.
    A General XML Validator
    To help you check the syntax of your XML files, we have created an XML validator to syntax-check your XML.
    XML Validator
    XML Errors Will Stop You