Java Foundation Note 2016/7/10

Source: Internet
Author: User

The role of import in Java

The Import keyword is used for importing packages, because in real-world development, you can't put all classes in the same default package. The purpose of a package is to sort the Java classes, and Java classes of different business logic are placed in the same package. such as toolkits, physical packages.

So, if you're in Class A in package A, you want to call Class B in package B, you need to import the B package.

For example: Import java.awt.*;

is to import all the classes in the java.awt package.

Import java.util.*

Its role is to import the full utility (Utility) library, which is part of the standard Java Development Kit.
In the Java.util library, there are many common interfaces and classes. such as collections (LIST,MAP, etc.)

In a program, usually the first line is the package xx.xx.xx;
Then the following is the import xx.xx.xx;

Package xx.xx.xx; The meaning of this sentence is to put the class into xx.xx.xx;
Why use a bag?
Package, one in order to resolve access rights issues. Like each class, method, property before the private, public, protect, default, this is the permission, the specific you can check online.
Package, but also to separate the code, the benefit is better management of code, increase the readability of the code and so on.

When there is a package, only import xx.xx.xx;
When you want to use the classes in other packages, you need to import the other packages so that you can access the classes in the other packages xx.xx.xx.
There are two ways to import packages, in your case, suppose you use the list in the Java.util package, then we can import:
a) import java.util.*; Import the entire Java.util library, you can understand, * is a wildcard, java.util.* is java.util all the things below;
b) Import java.util.list;//This just imports the list interface in the Java.util library. If you want to use the map in java.util, you need to import it separately.

The Java package acts like the namespace of C + +, preventing collisions between classes with the same name, which is not what we want.

Java Foundation Note 2016/7/10

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.