Java Package quick start tutorial

Source: Internet
Author: User

Java Package quick start tutorial

Why is Java Package designed? If you have never thought about it, I may provide a perspective here.

Imagine that, as a language designer, you must consider the conflict of variable names. To solve this problem, C ++ introduced namespace and Java introduced package.

1. variable name conflicts

All the software programming we normally use is basically stored as the basic unit of files, so we will discuss it in the file dimension below.

  • Within the same file: variable names in the same file conflict, which can be controlled by compilation. If the compilation phase detects repeated declarations of two variables, an error can be reported to the developer.
  • Different files: when small files A and B edit two files A. cpp and B. cpp respectively, the variable names in these two files cannot be repeated. For example, both files exist.int bVariable, then both small A and Small B can normally compile. cpp and B. cpp, so here small A and Small B won't immediately find the problem; but when small C references. cpp and B. cpp. errors may occur when these two files are merged and compiled. At this time, A and B may have gone out to play... This is different from the single-file compilation error. This is the source code developed by two different developers. It is best not to modify it in small C.
2. propose a solution to the conflict

What we are facing now is to solve the problem of small C. We have the following two solutions:

  1. Call back small A and Small B and tell them that they need to notify each other when declaring the variable again, and now immediately change the problem they have caused, this avoids variable name conflicts manually.
  2. A compilation mechanism is introduced to add "file name." To the variable name in the compiled file during file compilation. For example, int B in A. cpp is identifiedint A.bIn this way, as long as the file name is not repeated, you do not have to worry about all the variables in the two files will be repeated. As for the file name, you can leave it to the OS file system to judge whether the file is repeated.

The selection of this solution is not very difficult. Normal people will choose 2nd, but in fact the 2nd solutions still need to be improved, but our general direction is correct.

3. Solution Optimization

Adding an identifier prefix (file name.) to the variable name is indeed a method, which is called the prefix. However, the above scheme will only automatically append the prefix in the compilation phase, which will lead to A problem: If I want to reference A. cpp in c. cppint bVariable. What should I do?

Therefore, we can apply the prefix Method to the Code compiling stage, instead of the compilation stage. What does it mean? For example:

A. When compiling cpp, all variables must be prefixed with A prefix (for the moment, the name of the file). Therefore, the int B written earlier should be changedint A.bNote: This is changedint A.bInstead of the compile time, this time should be distinguished.

In this way, I can directly use the. B variable in the C. cpp file. By transferring the prefix Method to the editing stage, multiple files can reference variables and methods to each other without causing variable or method name conflicts.

4. Solution instance

The prefix method is a little more accomplished now. It solves naming conflicts among multiple files, but there are still some problems.

The prefix is specified as "file name. "In fact, it is not safe, because we know that the file system in the same directory will require that there be no duplicate files, but different directories can be used, so/Usr/A may exist. cpp and/Dev/. an error occurs when the cpp files are merged and compiled.

Likewise, it is obvious that we can provide N solutions. Here we provide three solutions:

  1. We should not equate prefixes with file names. We can specify different values for each file prefix. How can this problem be solved? Use a keyword (such as namespace) in each code file to identify the prefix of the file. The format is namespace devA or namespace usrA, in this way, two files with the same file name are given different prefixes, and they can also be referenced by each other.

  2. We still tie the prefix and the file name together, but this time, I am a little embarrassed to bind the folder. What do you mean? That is, the variables of/Usr/A. cpp are writtenint usr.A.bIn this way, the prefix is combined with the file level, which can solve the problem perfectly.

  3. We should not consider the prefix or the prefix in the file. We only need to dynamically specify the prefix in the Cross-file reference. What does that mean? Is the variable in the/usr/A. cpp and/usr/A. cpp files.int bOr writeint bBut when C. cpp references the two of them, they specify the prefix. See the following:

Imort/usr/A. cpp userA;

Imort/dev/A. cpp decA;

Print userA. B -- reference the variable here

Print devA. B -- reference the variable here

In fact, these three are examples of solutions corresponding to the C ++ namespace, Java Packge mechanism, and Nodejs namespace respectively.

Note: the differences between the C ++ namespace and the Java Package can also be seen here. In the namespace, only the namespace in each file is different, and it has nothing to do with the file name and path of the physical disk; java Pakage is associated with the file level, so it is related to the physical storage level.

4. Summary of package Mechanism

Since we are entitled to a Java Package, we should continue on the second solution.

For a Java class, its unique identifier is package + class name, for example, com. test. the package keyword indicates that the Package prefix of the Test class is com. test. During compilation, we use the following command to compile:

/ > javac Test.java

Using this command, a Test is generated under the root directory. class file. At this time, we noticed that the Package compilation of Test does not have a corresponding relationship with the level of the file system, the package hierarchy indicates the path where the Test class should be located, so that the jvm can find it. Here, you can create a directory/com/test/by yourself and put Test. class in this directory. Then, return to the root directory and executejava com.test.Test.

In addition, the package in Test already specifies the hierarchy. In fact, javac can automatically generate the corresponding File hierarchy and Test. put the class in, eliminating the trouble of manual movement, that is, add a-d directory name after javac.

javac -d ./ com.test.Test

5. End

I was not clear about the combination of javac and package. I have learned a lot from this derivation. By the way, I hope to help you.

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.