Java programming ideas all objects

Source: Internet
Author: User

2.2.3 arrays in Java

When creating an array object, you actually create a reference array, and each reference is automatically initialized as a specific value. This value has its own keyword null. Once Java sees null, it will know that this reference has not pointed to an object. Before using any reference, you must create an object for it;

 

2.3 never need to destroy objects

2.3.1 scope: Scope determines the visibility and lifecycle of variable names defined in it. variables defined in scope can only be used before the end of the scope.

2.3.2 scope of objects: Java objects do not have the same lifecycle as basic types. When a Java object is created with new, it can survive beyond the scope. So assume that you use the following code: {string S = new string ("A string");} // end of Scope

The referenced s disappears at the end of the scope. However, the string object pointed by s continues to occupy memory space.

Java has a "Garbage Collector" to monitor all objects created with new and identify objects that will not be referenced again. Then, the memory space of these objects is released for use by other new objects. This eliminates the "Memory leakage" issue, which is caused by the programmer's forgetting to release the memory.

 

2.4 create a new data type: Class

2.5 Methods, parameters, and returned values

Many programming languages (such as C and C ++) use the term "function" to describe the name subroutine; in Java, the term "method" is often used to indicate the "way to do something ".

The Java method determines what messages an object can receive.

The basic components of a method include the name, parameter, return value, and method body. The return type refers to the type of data returned after the method is called. The parameter list shows the type and name of the information to be passed to the method. The method name and parameter list are uniquely identified.

For example, if there is a method F () without any parameters, the return type is int. if an object named a can call f (), it can be written as: int x =. F ();

The type of the returned value must be compatible with the X type. This call method is often called "sending a message to an object ". In the preceding example, the message is F () and the object is. Object-Oriented Programming is generally simply classified as "sending messages to objects ".

 

If the return type is void, the return keyword is only used to exit the method.

 

2.6 construct a Java program

2.6.3 static keyword

Generally, when creating a class, it is to describe the appearance and behavior of the Class Object. Unless new is used to create the class object, nothing is actually obtained. When a new object is executed to create an object, the data storage space is allocated and its method is called by the outside world.

However, the above methods cannot be used in two cases. One scenario is that you only want to allocate a bucket for a specific data, regardless of the number of objects to be created, or even no objects. Another scenario is to expect a method not to be associated with any object of the class containing it. That is to say, a method can be called even if no object is created. The static keyword can meet the above two needs.

When a thing is declared as static, it means that the data or method will not be associated with any object instance of the class containing it.

 

There are two methods to reference static variables. You can use an object to locate it, or directly reference it using its class name (this method is recommended), which is not applicable to non-static members.

An important usage of the static method is that it can be called without creating any object. As we will see, this is very important for defining the main () method, which is the entry point for running an application.

Like any other method, the static method can create or use named objects of the same type. Therefore, the static method is often used as the "Shepherd" role, it is responsible for monitoring instances of the same type as it belongs.

 

2.7 your first Java program

At the beginning of each program file, the import statement must be declared to introduce all the additional classes required in the file code. Note: Here they are "extra" because a specific class is automatically imported to every Java file: Java. Lang.

If you do not know which class library a specific class is in, you can select "Tree" in the Java document to view every class provided by Java.

The parameter of the main () method is an array of string objects. Some programs do not use ARGs, but the Java compiler requires this because ARGs is used to store command line parameters.

 

2.8 comments and embedded documents

2.8.1 comments

A better design of the Java language is: writing program code is not the only important thing-writing documents is as important as the program code itself. The biggest problem in writing code documents is probably the maintenance of the documents.

If the document is separated from the code, you need to modify the corresponding document every time you modify the code, which is quite troublesome. The solution seems simple: "Link" the Code with the document.

To achieve this, the simplest way is to put everything in the same file and use a special annotation syntax to mark the document. In addition, a tool is required, it is used to extract comments and convert them into useful forms. This is exactly what JAVA does.

Javadoc is a tool used to extract comments. It is part of JDK installation. It uses some Java compiler technology to find special tag comments in the program. It not only analyzes the information marked by these labels, but also extracts the class name or method name adjacent to the annotation. In this way, we can generate a very good program document with the minimum workload.

Javadoc outputs an HTML file that can be viewed in a web browser. This tool allows us to create and maintain a single source file and automatically generate useful documents. With javadoc, we have the standards for document creation. We can expect that all Java class libraries should provide relevant documents.

In addition, if you want to perform special operations on the information processed by javadoc (for example, output in different formats ), you can write your own javadoc processor called "doclets. For more information about doclets, see Chapter 15th.

Below, we will only briefly introduce and outline the basic javadoc. The full and detailed descriptions can be found in the downloadable JDK documentation provided by java.sun.com. (Note that this document is not packaged with JDK and must be downloaded separately .) Decompress the document and check the "tooldocs" subdirectory (or click the "tooldocs" link ).

I have a question: does the "downloadable JDK document" mentioned here refer to the JDK API document? No, because I couldn't find the "decompress this document and check the" tooldocs "subdirectory ". So where can I download it?

 

2.8.2 syntax

All javadoc commands can only appear in the "/**" comment. Normally, the Comment ends with "*/". There are two main methods to use javadoc: Embed HTML or use "document tag ". The "independent document tag" is a command that starts with the "@" character and must be placed at the beginning of the comment line (but not before the beginning after the "*" character ). The "Line file tag" can appear anywhere in the javadoc comment. They also start with the "@" character, but should be included in curly brackets.

There are three types of annotation documents, which correspond to the three elements after the annotation position: Class, variable, and method. That is to say, the class comment is just before the class definition; the variable comment is just before the variable definition; and the method comment is just before the method definition. As shown in the following simple example:

/** A class comment */

Public class doctest {

/** A variable comment */

Public int I;

/** A method comment */

Public void F (){}

}

Note that javadoc can only annotate documents for "public" and "protected" members. "Private" and the comments that can be accessed in the package will be ignored, so they will not be seen in the output results (however, they can be marked with "-private, to include the comments of Private Members ). This makes sense because only public and protected members can be used outside the file, which is expected by the client programmer. All comments to the class are included in the output result.

 

2.8.3 embedded HTML

2.8.4 some tag examples

 

2.9 encoding Style

2.10 conclusion

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.