- 雙語版Java程序設(shè)計
- 何月順主編
- 656字
- 2018-12-27 20:14:10
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.
- 工業(yè)機器人虛擬仿真實例教程:KUKA.Sim Pro(全彩版)
- 圖解PLC控制系統(tǒng)梯形圖和語句表
- Windows 8應(yīng)用開發(fā)實戰(zhàn)
- MicroPython Projects
- 分布式多媒體計算機系統(tǒng)
- Learning C for Arduino
- 工業(yè)機器人運動仿真編程實踐:基于Android和OpenGL
- 悟透AutoCAD 2009案例自學(xué)手冊
- 激光選區(qū)熔化3D打印技術(shù)
- 計算機與信息技術(shù)基礎(chǔ)上機指導(dǎo)
- 精通數(shù)據(jù)科學(xué):從線性回歸到深度學(xué)習(xí)
- INSTANT Puppet 3 Starter
- 學(xué)練一本通:51單片機應(yīng)用技術(shù)
- Visual Basic項目開發(fā)案例精粹
- 運動控制系統(tǒng)