官术网_书友最值得收藏!

2.3 Write the First Java Program

A class definition has been created as a prototype. It can be used as a template for creating new classes that add functionality. Objects are programming units of a particular class. Dynamic loading implies that applications can request new objects of a particular class to be supplied on an “as needed” basis. Objects provide the extremely useful benefit of reusable code that minimizes development time.

對象是類的具體實現(xiàn)。動態(tài)加載意味著應(yīng)用程序能夠在需要的時候請求一個特殊類的實例。

A Java application resembles programs in most compiled languages. Code in object format resides on the user’s machine and is executed by a run-time interpreter that normally has to be user installed.

You wrote a simple ‘hello world!’ application to test the development environment. Now comes the explanation of the basic structure of Java applications using it as an example. Applications are stand-alone and are invoked (or executed) by using a Java interpreter.

/**
 * The HelloWorldApp 類實現(xiàn)一個應(yīng)用
 * 顯示"Hello World!".
 */
public class HelloWorldApp {
   public static void main(String[] args) {
            System.out.println("Hello World!");
   }
}

The first four lines is a comment on the application’s function. Comments are not required but are extremely useful as documentation within the source. Other notes and doc files may get lost but these stay right where they are most useful. A long comment starts with a /* or /** and ends with a */ .Short one line comments begin with // and end with the <return>.

The fifth line starts with the reserved word public. This indicates that objects outside the object can invoke (or use) it. The reserved word class indicates that we are building a new object with the name that follows. HelloWorldApp is the object name (your choice) and is case sensitive. Java ‘style’is to capitalize the first letter of each word only. The line concludes with an opening curly bracket that marks the start of the object definition.

注釋對于理解代碼有很重要的作用,有單行注釋和多行注釋。public修飾符表示其他對象可以調(diào)用該程序。HelloWorldApp是一個類名字,而該類的類體包含在花括號內(nèi)。

Line six is an invocation of an initialization method (or procedure in older languages). Static indicates that it calls the class and not an ‘instance’ of the class. The concept of ‘instance’ will be discussed later in the tutorials. The method’s name is main and the reserved word void indicates that no result is returned back. Main has arguments (aka parameters) in round brackets. String[] indicates the variable type is an array and args is a reserved word indicating that it is the command line values that are being passed over to main. The line finishes with an opening bracket for the main method.

main函數(shù)是該程序的主函數(shù),也是程序執(zhí)行的入口點。main函數(shù)沒有返回類型,但是具有一個參數(shù)String數(shù)組,該數(shù)組可以接收來自控制臺的輸入?yún)?shù)。

Line eight invokes the println method of the system.out object. What is to be printed is passed in the argument as a string parameter. Note that each Java statement concludes with a semicolon.

Finally closing curly brackets are used for the main and for the object definition.

主站蜘蛛池模板: 双峰县| 尼玛县| 牟定县| 马尔康县| 威远县| 卢氏县| 岳阳县| 龙岩市| 通州区| 西宁市| 永泰县| 武乡县| 黎平县| 武穴市| 鹿泉市| 轮台县| 锦屏县| 东辽县| 上思县| 平泉县| 邵阳县| 铜川市| 昌黎县| 广汉市| 黑河市| 安平县| 高雄市| 德昌县| 保德县| 和林格尔县| 三门县| 咸阳市| 肇庆市| 华蓥市| 峡江县| 湟中县| 晴隆县| 湘西| 普兰店市| 新绛县| 洛扎县|