Java Package and Import

Source: Internet
Author: User

Some people use Java for a while, but they don't know much about Java's package and import. Many people think that the original code. The import in the Java file causes the compiler to write all of the import programs to the compiled. class file, or think that import is similar to the #include of C + +, in fact, this is the wrong idea.

The role of Import

If not import , we need to write the full path name of the class when we use the Java class Library, such as the following code:

    java.util.ArrayList<Integer> list = new java.util.ArrayList<Integer>();

Every time this write to more than a lot of characters, programmers are very lazy, natural people can lazy, so there is import , just to the beginning of the Java file to import the corresponding class name once, then only need to write the class name can refer to the class, for example:

    import java.util.ArrayList;    ...    ArrayList<Integer> list = new ArrayList<Integer>();

The above is java.util a package, ArrayList is a class inside the package,

You can also use wildcard characters to refer to a package, so you can be lazy, but use as little as possible.

    import java.util.*;

At compile time, all class names are replaced with the corresponding full pathname, as ArrayList compared to the equivalent java.util.ArrayList , which is the same as when we write our own full path name.

importwhen looking for a class, follow the path specified in the environment variable CLASSPATH (if there are multiple, do the following), use the path as the root, and then import replace the path delimiter in the following full path name with the . .java suffix, The root directory and the replaced path are stitched together into full paths to locate the class file to be referenced. For example, the above imported java.util.ArrayList class file path is $CLASSPATH/java/util/ArrayList.java .

The role of the package

A package is used to bring together a group of classes that are connected and grouped together to accomplish a function, such as a library of tools in Java, usually a library consisting of multiple class, in order to facilitate organization we put this set of class in the same directory, This directory is considered to be a Java package (and of course it needs to be declared at the beginning of the Java file).

For example, the package name is like our surname, and the class name is like our name. There are a lot . of package names, as if they were compound surname. For example, Java.lang.String, is compound surname Java.lang, named String category, Java.io.InputStream is compound surname Java.io, named InputStream category.

But in practice, often the name of the case, it is necessary to use a different surname as a distinction, such as a wrote a String class, B also wrote a String class, in order to distinguish, can be used to A.String B.String express their respective classes and respectively String . By convention, Java is accustomed to using the reverse order of the Internet domain name as the package name, for example, I have a domain name java.oracle.com , then I write the String package name of the class is com.oracle.java .

    package com.oracle.java;    public class String{        ...    }

Using the Internet domain name reverse order as the package name, is not mandatory, you can use other names, as long as possible to ensure that the package name is unique, so it is common to use the path name as the package name, using the operating system directory hierarchy to differentiate. For example, I write a String class that is placed in the $CLASSPATH/java/util/ directory, so the package name is java/util .

    package java.util;    public class String{        ...    }
Actual combat code

CLASSPATHThe environment variable containsF:/WorkDirectory/

The full path is a F:/WorkDirectory/net/wuhui/util/MyString.java class file whose package name should be net.wuhui.util , so that the compiler can locate the MyString class exactly.

    package net.wuhui.util;    publicclass MyString{        System.out.println("MyString");    }

Test the class F:/WorkDirectory/test/Test.java , import the above custom MyString class, and use him for it.

    import net.wuhui.util.MyString;    publicclass Test{        publicstaticvoidmain(String [] args){            new MyString();        }    }

There are incorrect places, welcome to point out, extremely grateful!

Java Package and Import

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.