Read Java Object-oriented programming (Sun Weichen)

Source: Internet
Author: User

2.1 Creating a Java source file

A Java application consists of one or more files with the extension ". Java", which are referred to as Java source files, which are referred to as compilation units from the point of compilation.

This chapter contains two Java source files: Doll.java and Appmain.java, and the following example is their code.

2.1.1 Java source file structure

A Java application contains one or more Java source files, and each Java source file can contain only the following (except spaces and annotations):

① 0 or one package declaration statement

② 0 or more package introduction statements

③ declaration of 0 or more classes

④ 0 or more interface declarations

Each Java source file can contain definitions for multiple classes or interfaces, but at most only one class or interface is public, and the Java source file must be named with the name of the class with the public type.

2.1.2 Package Declaration Statement

A package declaration statement is used to place a Java class in a specific package and can have at most one packages statement, but the packages statement is not required. If you do not provide a package statement, it indicates that the Java class is in the default bundle and that the default package does not have a name.

The package statement must be in the first row of the Java source file.

1. The role of the package:

① can distinguish between classes with the same name.

② helps enforce access control.

③ helps to divide and organize classes in Java applications.

2. Naming conventions for packages

The name of the package is usually lowercase.

Java Basic Package provided by 3.JDK

Mainly include:

①java.lang Package--Contains thread classes, exception classes, System classes, integer classes, and string classes.

②JAVA.AWT Bag--abstract window tool bags

The ③java.io package-input/output package contains various input stream classes and output stream classes.

④java.util Package--provides some practical classes.

⑤java.net Package--Support TCP/IP network protocol

The classes in all of the JDK's packages make up the Java class Library, or the J2SE API. User-created Java applications are dependent on the J2SE API.

2.1.3 Packet Introduction statement

If a class accesses a class from another package, the former must introduce the class through an import statement.

With regard to the introduction of packages, it is important to note:

① if a class introduces two classes with the same name from different packages, the two classes must be distinguished by the class's full class name in the program.

② Although the symbol in the package name "." Can reflect the hierarchy between packages, but each package is independent, and the top-level package does not contain the classes in the child package.

The order of the ③package and import statements is fixed, in the Java source file, the package statement must be on the first line, followed by the import statement, followed by the declaration of the class or interface.

Declaration of the 2.1.4 Method

In the Java language, each method belongs to a particular class, and the declaration of the method must be in the declaration of the class, in the form of a declaration method:

Return value type  method name (parameter list) {   method body}

The method name is any valid identifier. The parameter list can contain 0 or more parameters, separated by a comma "," between the parameters.

The return statement must return data of the corresponding type.

The return statement has two functions:

① end execution of this method

② returns data to the caller of this method.

Declaration of the 2.1.5 Program entry main () method

The main () method is the entry point of the Java application each Java application runs from the main () method and must conform to 4 conditions

① Access Restrictions: public

② static method: Statics

③ parameter limit: Main (string[] args)

④ return type: void

The following can be used as a program entry method

Because the static modified method defaults to the final type, it is also possible to precede the main () method with the final modifier.

2.1.8 keywords

The Java language keyword is a special character in program code

Keywords for Java statements include:

⑴ declarations for classes and interfaces--class,extends,implements,interface

⑵ packet ingestion and package declaration--import,package

⑶ Data Type--boolean,byte,char,double,float,int,long,short

⑷ optional values for some data types--false,true,null

⑸ Process Control--break,case,continue,default,do,else,for,if,return,switch,while

⑹ Exception Handling--catch,finally,throw,throws,try

⑺ modifier--abstract,final,native,private,protected,public,static,synchronized.trnasient,volatile

⑻ operator--instanceof

⑼ Creating Objects--new

⑽ Reference--this,super

⑾ method return type--void

Reserved words for the Java language include: const and GOTO

To use the Java language keyword, be aware that:

The programming specifications are as follows:

2.2 Managing Java applications with JDK

The JDK mainly includes the following content:

Java Virtual Machine: responsible for parsing and executing java.

2.2.3 Running Java programs

The Java command is used to run a Java program that starts a Java virtual machine, loads the associated class, and then calls the main program class's main () method.

2.3 Javadoc Documentation

The address of the Javadoc document is:

2.4Java Virtual Machine run-time data area

Read the SUMMARY:

Readers should be familiar with the following:

Read Java Object-oriented programming (Sun Weichen)

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.