第10節 - Where is main() method in Android?
這一節我會介紹Android開始執行程式時,須要執行的檔案。
如果大家有學習Java,Java是須要有一個main() method去執行程式。我在第5節 - 用Eclipse寫Java程式示範過用System.out.println(),它主要用來把文字顯示在螢幕上。System.out.println()就是包括在main() method 之內。

但是Android和Java不同,Android是不須要有main() method去執行程式的,Android會用你第一個程式(例如:HelloWorld extends Activity那個),Android就是用HelloWorld內的onCreate() method去執行程式。

Android沒有main() method的最好解釋(Best Description)
首先我會把我閱讀過Android沒有main() method最好的解釋記錄下來:
1. Java
In core Java programs we need a main() method, because while executing the byte code the JVM(Java Virtual Machine) will search for the main() method in the class and start executing there.2. Android
In the case of Android, the DVM(Dalvik Virtual Machine) is designed to find a class which is a subclass of Activity(extends Activity) and which is set as a LAUNCHER(in AndroidManifest.xml) to start the execution of the application from its onCreate() method, so there is no need of a main() method.

Android沒有main() method詳解
看完以上其他人的解釋後,想信大家會對Android沒有main() method有了初步了解,以下部分我會加以解釋。
首先Java須要main() method是因為Java是用JVM,而JVM在設計上是會去尋找main() method執行程式,想信大家對Java須要main() method應該明白了。

1-大家可以用Windows Notepad輸入圖片內的Java Hello World程式,main() method就包括在內。
2-注意,如果我們的程式有多過一個class,所有的class檔案就會打包(Packaging)在一個.jar文件夾內,.jar是一個Package File Format文件夾。
3-最後,JVM會在HelloWorld.class內尋找main() method執行程式。
至於Android,Android在設計上不是用JVM(Java Virtual Machine),而是用DVM(Dalvik Virtual Machine),而DVM在設計上是會去尋找onCreate() method執行程式。

1-大家可以用Eclipse輸入圖片內的Android Hello World程式,onCreate() method就包括在內。
2-和Java不同,這次HelloWorld.class不是給JVM用,而是給DVM用,所以要用一個叫DX Tool的工具把class 檔案轉為.dex格式(Dalvik executable file ),DX Tool是包括在Android SDK內。
3-除了.dex檔案,其他檔案包括Data files和Resources(例如圖片、音樂檔案等等),就要用一個叫AAPT (Android Asset Packaging Tool)的工具把.dex、Data Files和Resources檔案打包(Packaging),而不是改變其格式, AAPT也是包括在Android SDK內。
4-最後,DVM會在Android的AndroidManifest.xml檔案內驗查那一個程式繼承自Activity class,在以上圖片內我們就有HelloWorld 繼承 Activity (HelloWorld extends Activity),所以就會執行HelloWorld內的onCreate() method。
最後,大家在上圖可以明白,雖然Android是用Java寫成,但Android不用JVM,而用DVM,所以Android是沒有main() method的。