Differences between package and Import & Java application and Java Applet

Source: Internet
Author: User

Some people write Java for a while, but they still don't know much about Java package and import. Many people think that the import in the original code. Java file is similar to the # include in C/C ++. This is actually a wrong idea.

Let's take a look at the usefulness of Java package. In fact, the package name is like our surname, and the class name is like our name. There can be many package names, like a surname. For example, java. Lang. String is the class with the surname java. Lang and the name is string.

(1) The reason why Java uses the package mechanism is also very obvious, just like we get a name, just in the same school, there may be many students with the same name. If the last name is not used, the school may be dealing with student data or the names of students. But the problem arises, because many suites have long names. When writing a program, they will write more words and spend a lot of time, for example:

Java. Io. inputstream is = java. Lang. system. In;
Java. Io. inputstreamreader ISR = new java. Io. inputstreamreader (is );
Java. Io. bufferedreader BR = new java. Io. bufferedreader (ISR );

It is not beautiful and troublesome. So Sun thought of a solution, namely import. This import is the abbreviation of the classes used in the program at the beginning of the program. First, write at the beginning of the file:

Import java. Lang. system;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. bufferedreader;

These lines illustrate the categories of these four names, which are called only by name in the program. Therefore, when the program mentions that system refers to Java. lang. system, and inputstream refers to Java. io. inputstream, and so on. So the original program becomes:

Inputstream is = system. In;
Inputstreamreader ISR = new inputstreamreader (is );
Bufferedreader BR = new bufferedreader (ISR );

(2) Does it seem much refreshed? If these categories are used many times, you can better understand the benefits of import. However, this is not enough. Some people may think it is a waste of time to create too many imports, So Sun provides another method:

Import java. Lang .*;
Import java. Io .*;

That is, wait for the class without a surname mentioned in the program, not Java. lang is the name of Java. io. If the two classes have the same name, and unfortunately you only use the class name, the compiler will still complain to you, because it still does not know the class you are talking about refers to the class of that surname.

Why is import different from # include in the first place? Since the import function ends here, it does not load the file content like # include. The import only requires the compiler to help you type, so that the compiler will not write the program code of other files into the class without a surname plus the surname.

In addition, Sun helped us to be a little more lazy. Because Java. lang is too often and too often used, and almost no program needs to use it, so whether or not you write the import Java. lang;, the compiler will automatically help you complete, that is to say, the compiler will automatically go to Java as long as it sees a class without a surname. lang, so we don't need to import Java. lang.

(3) After importing a class, you can directly create a class name type instance in the source code. If you want to reference constants in a class, such

Import static android. view. windowmanagerpolicy. off_because_of_prox_sensor; if only one constant is used, precise import will be better. off_because_of_prox_sensor can be directly used in the Code. If no precise import is made, windowmanagerpolicy is used. off_because_of_prox_sensor; if you reference a function in the class, you can directly reference the function name.

========================================================== ========================================================== ==================================

Java is a semi-compiled and semi-interpreted language. Java User Programs are divided into two types: Java application and Java applet. These two types of programs have some differences in the composition structure and execution mechanism, mainly reflected in the following aspects:
(1) The running mode is different. Java application is a complete program that can run independently. Java Applet cannot run independently. It must be embedded into a web page written in HTML, control execution through a Java-compatible browser.
(2) Different running tools. After the Java application program is compiled, it can be interpreted and executed with a common Java interpreter. Java Applet can only be executed through a Web browser or applet observer.
(3) The program structure is different. Each Java application must contain one and only one main method. When executing a program, you must first find the main method and run it as the entry point. The class that contains the main method is often called the main class, that is, the Java application program contains a main class. The applet does not have the main class containing the main method, which is exactly why the applet cannot run independently. Although the applet does not have a main class containing the main method, the applet must have a class derived from Java. Applet. Applet, which is provided by the java system.
(4) The limitations are different. Java application programs can be designed to perform various operations, including file read/write operations. However, Java Applet neither reads nor writes the disk files on the site. However, due to the introduction of Applet, web pages have dynamic multimedia effects and interactive performance, this enables a Web page written in HTML language named Hypertext and plain text to have the hypertext function, which not only displays text information, in addition, it can have a variety of image effects and dynamic graphics effects to make the page appear vivid and beautiful. In addition, the applet adds buttons and other functions to the web page, thus increasing interaction.

Http://xiazai.zol.com.cn/article_topic/79/799369.html ()

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.