Java namespace origin and introduction

Source: Internet
Author: User

Name visibility)

Name management is an important issue for any programming language. If you use
One name, and others use the same name in another module of the program. How can this problem be distinguished?
What if the two names do not conflict with each other? This problem is especially serious in C, because the program often contains many
Difficult to manage. C ++ class (Java class is based on this) to wrap functions in it, thus avoiding
Conflict. However, the existence of global data and global functions is still allowed in C ++, so conflicts may still occur. To solve
In this case, C ++ introduces the concept of "namespace" through several keywords.
 
Java adopts a brand new method to avoid all the above problems. To generate a class library
Obfuscated name. Java uses a specifier similar to an Internet domain name. In fact, Java designers want programmers
Come and use your own Internet domain names, because this ensures they are unique. Because my domain name is
Bruceeckel.com, so my various strange application tool libraries are named
Com. bruceeckel. Utility. foibles. After the domain name is reversed, the period indicates the division of sub-directories.

Designers found this
This will cause some problems. Therefore,Now the entire package name is in lower case..
 
This mechanism of Java2 means that all files can automatically survive in their own namespace, and the same
Each class in the file has a unique identifier. So you don't have to learn special language knowledge to solve this problem-Java
The language itself has already solved this problem for you.

 

Get the inspiration for the corresponding namespace Based on the Reverse Domain Name

 

Use other components

 

If you want to use pre-defined classes in your program, the compiler must know how to locate them. Of course,
This class may be in the source file that sends the call; in this case, you can directly use this class-that is
This class is defined only after the file (Java eliminates the issue of "Forward reference", so you don't have to consider it ).
 
What if the class is located in another file? You may think that the compiler should be smart enough

Find its location. But this is not the case. Imagine the following: If you want to use a class with a specific name,
There are more than one definition (assuming these definitions are different ). Worse, assume that you are writing a program
During the creation process, you want to add a new class to the class library, but it conflicts with an existing class name.
 
To solve this problem, you must eliminate all possible obfuscation situations. For this purpose, you can use keywords
Import to accurately tell the compiler what class you want. Import indicates that the compiler imports a package, that is,
Class Libraries (in other languages, a library not only contains classes, but also methods and data.
Code must be written in the class ).
 
Most of the time, we use the components in the java standard library attached with the compiler. With these components, you will not
A long string of Reverse Domain Names is required. For example, you only need to write as follows:
 
Import java. util. arraylist;
 
This line of code tells the compiler that you want to use the Java arraylist class. However, util contains a large number of classes, including
You want to use several of them, and do not want to explicitly declare them one by one. Then you can use the wildcard "*"
To achieve this goal easily:
 
Import java. util .*;

Using this method to import a group of classes at a time is more common than importing classes one by one.

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.