Preliminary Analysis of Java program structure

Source: Internet
Author: User

Complete JAVA SourceProgramStructure:

Package statement; // one more statement

Import Statement; // This part may have several or no import statements, which must be placed before all the class definitions

Public classdefinition; // public class definition section. At most, there is only one public class definition.
// The Java language requires that the file name of the Java source program must be exactly the same as that of the public class (Java in Japan, problematic brain shell)
Classdefinition; // class definition, which can have 0 or more class definitions

Interfacedefinition; // interface definition, which can be zero or multiple

 

Package statement: The Java compiler generates a bytecode file for each class, and the file name is the same as the class name. classes with the same name may conflict. To solve this problem, Java provides a package to manage the namespace. What is a package? Actually, it is a folder.

In the java system class library, classes with similar functions are placed in a package. For example, all graphical interfaces are stored in Java. in the AWT package, classes related to network functions are put in the java.net package. The class (the. Class file) written by the user should also be placed in the corresponding package named by the programmer according to the function.

In the command line, type "javac-d F:/javaproject helloworldapp. java, then compile the generated helloworldapp. the class file will be placed under the directory F:/javaproject/javawork/helloworld/. At this time, F:/javaprojcet is equivalent to path. However, if path is not specified during compilation, the generated. Class file will be placed under the current directory where the command line is located during compilation. For example, in the command line Directory F:/javaproject, enter the compilation command "javac helloworldapp. java, the generated helloworldapp. the class file will be placed under the directory F:/javaproject. The package statement does not work at this time.

 

However, if the program contains a package statement and you enter "javac-D path helloworldapp. Java" in the command line during compilation, you can run the program in either of the following ways:

First, enter the character "Java p1.p2. helloworldapp" in the path directory of the command line ".
Type 2: add the directory path to the environment variable classpath, and enter "Java p1.p2. helloworldapp" in any directory during the runtime.

However, you can only find the file in the directory where the file is located. The so-called package is actually the folder where the file is generated.

Import Statement:If Java is used in the source program. for classes other than the lang Package, both the system class and the class defined in the package must be identified by the Import Statement to notify the compiler to find the corresponding class file during compilation. The button belongs to the java. AWT package. during compilation, the compiler searches for the class button from the classpath/Java/AWT directory.

Java programming specifications:

Package Name:Package Names are all lowercase nouns separated by vertices, for example, java. AWT. event;

Class Name:The first letter is capitalized. Generally, a class name is composed of multiple words, and the first letter of each word must be capitalized, for example, class helloworldapp;

Interface Name:The naming rules are the same as the class names;

Method Name:It is often composed of multiple words. The first word is usually a verb, the first letter is lowercase, and the first letter of each word in the middle must be capitalized, for example: balanceaccount, isbuttonpressed;
Variable name:The name must be in lowercase. It is generally a noun, for example, length;
Constant Name:Constants of the basic data type are named in uppercase. If they are composed of multiple words, they can be separated by underscores (_), for example, int year, int week_of_month. If they are constants of the object type, it is a mix of upper and lower cases, separated by upper-case letters.


 

Related Article

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.