Taste first cup of coffee
First, notes and documents
The reason why Java annotations and document features are introduced in the top, because the concept of Java programs makes it.
Almost all programming languages provide a function of adding comments in the source code. Developers have made a memo or reminder via the reader of yours or code, write notes in the book when reading, or use n in the room Second post. Java language is a good idea to prepare source code is not the only important job, and the detailed note is equally important to add a detailed note. The sense is very simple, the source code and Java's bytecode are different. The bytecode is to the computer, and the source code is to read, no good documentation and comments, read the source code must guess the intention of the author of the source code, It is bound to be boring and the efficiency is low.
There are two comments in Java, and our Hello World! The sparrow is small but the five organs, all are used. Here is the source code of the Hello World! Program:
/ ** todaydo to change the template for this generated file go to * window - preferences - java - code style - code templates * // *** @Author gary chan ** Todo to change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class HelloWorldApp {public static void main (String [] args) {// print the words Hello World System.out.println! ("Hello World!");}}
The first annotation is inherited from the C language, comment from / * starts, to * / end, they must be used, and there is a wrap. Hello World! The beginning of the program is this style of annotation, which records the initial time and some prompts of this source program. Many programmers have added a * symbol before multi-line comments, thus forming the pattern above. Of course, the content of this comment is automatically generated by Eclipse, you can modify the automatically generated content by modifying the Eclipse code template. The second note is a single-line annotation style that inherits from the C language, and the comment starts from //, until the end of this line. This style of comments are very easy to use, you don't have to look down on the keyboard to find / and * keys, just press the / key twice. At the same time, it is not necessary to make a comment symbol pair like C language style notes. Hello World! // Print Hello World! Words is this style of comments.
If your insight is sensitive, you will find a Hello World! Program that is also a code wrapped in / ** and * / parcels and does not belong to any of the above comments. That's right, this is a Java Documentation. Different documents are different, and the comments are generally more casual, reflecting the functionality or declaration of the local statement, and the document is related to the structure of the code, the proportion is a summary of the code, giving a structured overall concept. In particular, the Java document comments commented in classes, variables, or methods followed.
We still combine Eclipse to illustrate the application of Java documents. First open the Hello World! Program in Eclipse. Try to move the mouse to the HelloWorldApp character, is it displayed as shown in Figure 1 (Figure 1 Eclipse automatically displays the function of the Java document)?
That's right, when you move your mouse to a Java class, variables, or methods, Eclipse will extract the appropriate Java documentation through code analysis techniques in the source code and display it to you, if the Java element has a document. Let's write Java documents for the main method to experience it. First move the cursor to the public class helloworldapp {and enter the car to insert a blank line. Then, enter / ** and enter, you will find that Eclipse automatically generates a Java document block, and automatically identifies the parameter called Args (very magic?). Remof this document into this: / *** This is the main function, and the program will start from here. * @Param args command line parameter * /
Then you move the mouse to the main, is it a clear white document information?
In fact, the Java document function is an important feature throughout the Java language. But when you move the cursor to System.Out.println ("Hello World!" Statement, you can't get a useful meaning and parameter usage, the statement is the Java built-in API. How can I like this?
Smart, you must think of the answer - must be the Java API's Java document is not configured. That's right, J2se SDK does not include Java API's Java documentation, you need to download separately. You can download a document from http://java.sun.com/j2se/1.4.2/download.html, if you download it from abroad, you can also go to http: //garychan.3322. ORG Java resource column download, about 32.80MB. After downloading, decompress the DOC directory in the compressed package to C: /J2SDK1.4.2_04/.
Eclipse uses JRE as Java virtual machine by default. However, using J2se SDK itself is a better choice, because you can enable important features of viewing Java API documents, which is important for improving software development efficiency and deep experience Eclipse. Methods as below:
1. Click Windows, Preferences in the Eclipse menu item.
2. Expand Java / Installed JRE, press Add, and press the parameters as shown in Figure 2 (Press OK button after Figure 2).
3. Press the J2SDK 1.4.2_04 in the Installed JRES dialog, press "OK" to close the Preferences dialog. Eclipse will prompt you to recompile once, "OK".
Ok, now move the cursor to the Println to see if it is the same as shown in Figure 3 (Figure 3)?
How is this method for detailed instructions? If the content is more, all information can be viewed after the F2 is locked. What api does not understand in the future, don't have to go online, the Java API document itself is the Java API's big encyclopedia, and then with the automatic display of Eclipse, it is not easy.
By the way, Java also provides mechanisms that automatically extract from the source code to generate an HTML file to read separately, which is a Javadoc tool provided by J2se SDK. In Eclipse, you can operate through the export option in the File menu.
Just do it
Try to change the HelloWorldApp's Java document and modify the Author property be your name. Then generate the Java document of the project via the Eclipse built-in JavaDoc output function, and feel the development concept of the Java document code.
Java documentation is an award. In addition to the PARAM and Author tags used above, Java documents also support many specific labels, you can directly embed HTML code to get a beautiful output, and can also extend Javadoc's processing mechanisms through Doclets. Complete instructions For documents, please refer to C: /J2SDK1.4.2_04/DOCS/ToolDocs/Windows/javadoc.html. Taste first cup of coffee
Second, class
When taste the first cup of coffee, we said that Hello World! Program defines a HelloWorldApp class (Class).
Class is the basic unit of the Java or the like. "A template that defines the relationship between the data and behavior from the instance of the class (instance). When you instantiate (instantiate), you will get a class of objects (Object), with all the data and behaviors of classes. Generally, we refer to the data of the class and objects to variables, and the behavior is called method. The Java language uses the Class keyword to define a class, and then a pair of braces define the variables and methods of the class. Our HelloWorldApp class has no variable, only one main method.
The relationship between classes and objects is very good. "The class is an abstract concept, better than" people ", and the object is a specific person, such as Bush, Putin, they have seven emotions (variables), all will eat food (Method) .
In the second series of "first cup of coffee", in order to avoid the concept of classes and objects, I directly called the HelloWorldApp as an object. Now you should understand that this statement is inaccurate, and helloworldapp is a class, while main is a method of this class. It is now officially dialing.
Taste first cup of coffee
Third, MAIN method
Each Java application must contain a main method, the syntax is as follows:
Public static void main (string [] args)
The main method contains three modifications:
(1) Public: Represents the MAIN method can be called by any object. (2) Static: Indicates this is a class method, which is different from the example method, and is independent of the specific object. (3) Void: Indicates that the main method has no return value.
The MAIN method is a very special way in the Java language, similar to the main function in C / C . When the Java parser performs Java applications, it first executes the MAIN method of this Java class. You should write the logic of the program, such as printing Hello World!, Written in main.
The main method has a parameter, which is args, which is a string array, and the operating system passes the command line parameters through this mechanism. Since the Hello World! Program ignores the command line parameters, so you don't discuss it here.
Taste first cup of coffee
Fourth, use class and object
Let us explain the use of class and objects in a Hello World! Program.
Hello World! The program is small, only the most basic HelloWorldApp class is defined. Even so, it does use other classes ?? System class:
System.out.println ("Hello World!");
The System class is one of the members of the Java API, providing the ability to operate the operating system-related functionality of the operating system, but it is true. OUT is the class variable of System (consistent with the meaning of the method mentioned earlier). In fact, all variables of System are class variables. All methods are class methods. Datherently modified with static, you can pass "." Operators directly. For example, the SYSTEM class has a getProperty class method to get the operating system properties, as for what operating system, you have no need to kiss. If the main method of the Hello World! Program is modified:
Public static void main (String [] args) {system.out.print ("Hello"); System.Get.Println ("User.Name"));} Run the program, you will get "Hello Gary The result of "CHAN". Oh, our program can recognize people, grow a lot!
The principle of the class variable will be said. When the Java application loads the System class, you can automatically initialize the PrintStream class to get a printStream object, and assign this object to the OUT variable. Thus, you call the PrintLn method of the OUT, which is actually an instance method of calling an object. In this way, Hello World! The word is output.
Summarize the class variables, class methods and instance variables, the relationship between instance methods:
(1) Class variables or class methods are related to specific classes, Java will assign a class variable for each class, regardless of this class. Class variables and class methods can be operated by class names. (2) Example variables or examples are related to specific objects (instances of classes), each object has an instance variable for its own dedicated. Example variables and instance methods can be operated by instance names.
Five, small knot
This time, we look at the Hello World! Program. Although a small Java program has so many doors, but do not experience the rainbow, I hope you can feel the Java language, so get into the Java's church.
Don't underestimate the importance of the Java document. Java has a lot of respect for respect. The so-called no rules are not square, you should understand and implement it, so you can write beautiful code.