Packages and documents in Java

Source: Internet
Author: User

What is a package

The package in Java (Package) actually refers to the directory, which is to better manage Java classes (class) and Interfaces (Interface). The Java language package can be used by another Java development package. If we want to refer to a class in a package, use the Import keyword to mark it. Like what:

import java.util.
date=new Date

Hint: Import Java.util. Represents that all public classes and interfaces in Java.util are introduced into the current package. The matching characters here can be called into multiple class names.

Common Java Standard Packages

The JDK provides us with a number of standard Java classes and interfaces that are necessary to write Java programs, know the classes and interfaces that each package contains, and are familiar with these classes and interfaces as basic skills that every Java programmer should master.

Common packages in Java are: Java.applet, java.awt, JAVA.I, Java.lang, java.net, and Java.util, and so on. The java.applet contains classes and interfaces for designing small applications (applets); java.awt is a window Toolbox package (awtabstract window Toolkit) with some GUI-interface-related classes java.io package support input and output, such as file input stream class: FileInputStream; Java.lang contains threads, exceptions, systems, integers, and other related classes, is the default load in Java program a package; java.net this class supports TCP/IP network protocols , and contains the socket class and URL-related classes, which are to be used in network programming; Java.util contains common classes of programs, such as Datedictionary, and so on.

In addition to some of the standard packages mentioned above, there are many other packages, such as database programming, we may need to use java.sql package, write network program will also use Java.rmi package (Rmiremote method invocation) and so on. In addition, the Javax. package is a number of standard package extensions, commonly used packages are: Javax.swing, Javax.sound.

Create a package yourself

Packages in Java can be created by themselves. We can put many functions similar to the class and interface in the same package, for easy management and use.

Basic ways to create packages

1. Define public classes;

2. The first sentence plus "Package package name";

3. Place the Java file generated class in a directory with the package name Directory name 

4. All public classes in this package can be accessed in other programs using the "Import package name".

Creation and use of packages

//filename B.java
package com.chen.test //定义一个包
public class B //定义一个类
public void addint iint j  //两个数求和,并输出
System.out.printlni+j

The code above is to create a simple package: com.chen.test, we name this file B.java. Now run javac-d c\ B.java compile the package (the command generates the Com\chen\test directory under C Disk and saves the compilation results in B.class). Now that we have entered the C\com\chen\test directory, we can see that the compiled class file is also included.

OK, now that the package has been created, what you need to do is set the c\com\chen\test in your environment variable CLASSPATH.

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.